-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
206 lines (174 loc) · 4.36 KB
/
logger.go
File metadata and controls
206 lines (174 loc) · 4.36 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package glog
import (
"context"
"fmt"
"io"
"os"
"time"
)
// Logger declares the logger.
type Logger struct {
ctx context.Context
// level set the minimum accepted level,
// less than the level will be ignored;
level Level
// timeLayout set the time format in log message.
timeLayout string
// caller set whether adds caller info in log message.
caller bool
// encoderFunc used to get a new encoder in log entry.
// Notes: change the encoderFunc will cause the fields empty and rebuild.
encoderFunc EncoderFunc
// fields add fixed field into every log entry
fields Encoder
// exporter used to export the log by every entry.Fire
exporter Exporter
// errorOutput is the error output writer of this logger
// logger will write error message into this while failed to log message
//
// NOTE: logger will not check returning error of this writer
errorOutput io.Writer
// isRoot indicates the logger whether is a root logger.
isRoot bool
}
func NewDefault() *Logger {
l := &Logger{
ctx: context.Background(),
level: DebugLevel,
timeLayout: defaultTimeLayout,
caller: false,
encoderFunc: TextEncoder,
fields: nil,
exporter: DefaultExporter,
errorOutput: os.Stderr,
isRoot: true,
}
l.fields = l.encoderFunc()
return l
}
// Context returns the ctx where in the logger.
func (l *Logger) Context() context.Context {
return l.ctx
}
// WithContext will reset logger's ctx.
func (l *Logger) WithContext(ctx context.Context) *Logger {
l.ctx = ctx
return l
}
// WithLevel will reset logger's level.
func (l *Logger) WithLevel(level Level) *Logger {
l.level = level
return l
}
// WithTimeLayout will reset logger's timeLayout.
func (l *Logger) WithTimeLayout(layout string) *Logger {
l.timeLayout = layout
return l
}
// WithCaller will reset logger's caller.
func (l *Logger) WithCaller(ok bool) *Logger {
l.caller = ok
return l
}
// WithExporter will reset logger's exporter.
func (l *Logger) WithExporter(exporter Exporter) *Logger {
l.exporter = exporter
return l
}
// WithEncoderFunc reset set logger's encoderFunc.
func (l *Logger) WithEncoderFunc(f EncoderFunc) *Logger {
l.encoderFunc = f
if l.fields != nil {
_ = l.fields.Close()
}
l.fields = f()
return l
}
// WithErrorOutput reset set logger's exporter.
func (l *Logger) WithErrorOutput(w io.Writer) *Logger {
l.errorOutput = w
return l
}
// WithFields for add fixed fields into the log entry.
func (l *Logger) WithFields() Encoder {
return l.fields
}
// ResetFields for clear the data in fields.
func (l *Logger) ResetFields() Encoder {
_ = l.fields.Close()
l.fields = l.encoderFunc()
return l.fields
}
// Clone do copy and returns a new logger.
func (l *Logger) Clone() *Logger {
nl := &Logger{
ctx: l.ctx,
level: l.level,
timeLayout: l.timeLayout,
caller: l.caller,
exporter: l.exporter,
encoderFunc: l.encoderFunc,
fields: l.encoderFunc(),
errorOutput: l.errorOutput,
isRoot: false,
}
err := nl.fields.WriteIn(l.fields.Bytes())
if err != nil {
_, _ = fmt.Fprintf(l.errorOutput, "[glog]: %s write fields fail when clone: %v\n", time.Now().Format(l.timeLayout), err)
}
return nl
}
// Close close the logger for releasing resources.
//
// Notes: Close will close the Exporter in root logger.
func (l *Logger) Close() error {
if l == nil {
return nil
}
var errs []error
if err := l.fields.Close(); err != nil {
errs = append(errs, err)
}
if l.isRoot {
// Close the exporter is the root logger.
if err := l.exporter.Close(); err != nil {
errs = append(errs, err)
}
}
l.ctx = nil
l.timeLayout = ""
l.encoderFunc = nil
l.fields = nil
l.exporter = nil
l.errorOutput = nil
if len(errs) == 0 {
return nil
}
return fmt.Errorf("%v", errs)
}
func (l *Logger) newEntry(level Level) *Entry {
if level >= l.level {
return newEntry(l, level)
}
return nil
}
// Debug returns an Entry with DebugLevel.
func (l *Logger) Debug() *Entry {
return l.newEntry(DebugLevel)
}
// Info returns an Entry with InfoLevel.
func (l *Logger) Info() *Entry {
return l.newEntry(InfoLevel)
}
// Warn returns an Entry with WarnLevel.
func (l *Logger) Warn() *Entry {
return l.newEntry(WarnLevel)
}
// Error returns an Entry with ErrorLevel.
func (l *Logger) Error() *Entry {
return l.newEntry(ErrorLevel)
}
// Fatal returns an Entry with FatalLevel.
func (l *Logger) Fatal() *Entry {
return l.newEntry(FatalLevel)
}