-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexporter.go
More file actions
127 lines (108 loc) · 2.84 KB
/
exporter.go
File metadata and controls
127 lines (108 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package glog
import (
"fmt"
"io"
"os"
)
var (
DefaultExporter = StandardExporter(NopWriterCloser(os.Stdout))
)
// Exporter used to handle the Entry.
type Exporter interface {
// Export NOTICE: The `data` will be reuse by put back to sync.Pool.
//
// Thus the `data` should be disposed after the `Export` returns.
// If the `data` is processed asynchronously, you should get data with Copy method.
Export(record *Record) error
// Close to close the exporter.
Close() error
}
var (
_ Exporter = (*standardExporter)(nil)
_ Exporter = (*matcherExporter)(nil)
_ Exporter = (*multipleExporter)(nil)
)
// StandardExporter return a Exporter implements by standardExporter.
// This used to write log record to writer.
func StandardExporter(w io.Writer) Exporter {
return &standardExporter{w: w}
}
// standardExporter creates an exporter that write log entry into an io.Writer.
type standardExporter struct {
w io.Writer
}
func (exp *standardExporter) Export(record *Record) error {
_, err := exp.w.Write(record.Bytes())
return err
}
// Close for close the Exporter.
func (exp *standardExporter) Close() error {
if c, ok := exp.w.(io.Closer); ok {
return c.Close()
}
return nil
}
// FilterExporter return a Exporter implements by matcherExporter;
// This used to write only the specified level of Entry.
func FilterExporter(w io.Writer, f Filter) Exporter {
return &matcherExporter{w: w, f: f}
}
// matcherExporter creates an exporter that write log entry into an io.Writer.
type matcherExporter struct {
w io.Writer
f Filter
}
func (exp *matcherExporter) Export(record *Record) error {
if !exp.f.Match(record.Level()) {
return nil
}
_, err := exp.w.Write(record.Bytes())
return err
}
// Close for close the Exporter.
func (exp *matcherExporter) Close() error {
if c, ok := exp.w.(io.Closer); ok {
return c.Close()
}
return nil
}
// MultipleExporter used for apply multiple Exporter to a Entry.
func MultipleExporter(exporters ...Exporter) Exporter {
return &multipleExporter{exporters: exporters}
}
type multipleExporter struct {
exporters []Exporter
}
func (exp *multipleExporter) Export(record *Record) error {
var errs []error
for i := range exp.exporters {
err := exp.exporters[i].Export(record)
if err != nil {
errs = append(errs, err)
}
}
if len(errs) == 0 {
return nil
}
return fmt.Errorf("%v", errs)
}
func (exp *multipleExporter) Close() error {
var errs []error
for i := range exp.exporters {
if e := exp.exporters[i].Close(); e != nil {
errs = append(errs, e)
}
}
if len(errs) == 0 {
return nil
}
return fmt.Errorf("%v", errs)
}
//// FileExporter is a FilterExporter wrapper that uses a file.
//func FileExporter(name string, f Filter) (Exporter, error) {
// w, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
// if err != nil {
// return nil, err
// }
// return FilterExporter(w, f), nil
//}