/
logger.go
209 lines (164 loc) · 7.15 KB
/
logger.go
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
207
208
209
package logger
import (
"context"
std "log"
)
// DefaultLogger is default logger.
var DefaultLogger Logger = New()
type Logger interface {
// Options The Logger options
Options() Option
// SetLevel set logger level
SetLevel(lv Level)
// WithContext with context
WithContext(ctx context.Context) Logger
// WithFields set fields to always be logged
WithFields(fields map[string]interface{}) Logger
// WithCallDepth with logger call depth.
WithCallDepth(callDepth int) Logger
// WithError with logger error
WithError(err error) Logger
// Debug uses fmt.Sprint to construct and log a message.
Debug(args ...interface{})
// Info uses fmt.Sprint to construct and log a message.
Info(args ...interface{})
// Warn uses fmt.Sprint to construct and log a message.
Warn(args ...interface{})
// Error uses fmt.Sprint to construct and log a message.
Error(args ...interface{})
// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
Fatal(args ...interface{})
// Debugf uses fmt.Sprintf to log a templated message.
Debugf(template string, args ...interface{})
// Infof uses fmt.Sprintf to log a templated message.
Infof(template string, args ...interface{})
// Warnf uses fmt.Sprintf to log a templated message.
Warnf(template string, args ...interface{})
// Errorf uses fmt.Sprintf to log a templated message.
Errorf(template string, args ...interface{})
// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
Fatalf(template string, args ...interface{})
// Debugw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
//
// When debug-level logging is disabled, this is much faster than
// s.With(keysAndValues).Debug(msg)
Debugw(msg string, keysAndValues ...interface{})
// Infow logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
Infow(msg string, keysAndValues ...interface{})
// Warnw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
Warnw(msg string, keysAndValues ...interface{})
// Errorw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
Errorw(msg string, keysAndValues ...interface{})
// Fatalw logs a message with some additional context, then calls os.Exit. The
// variadic key-value pairs are treated as they are in With.
Fatalw(msg string, keysAndValues ...interface{})
// Log writes a log entry
Log(level Level, template string, fmtArgs []interface{}, context []interface{})
StdLog() *std.Logger
// Sync logger sync
Sync() error
}
// InitLogger Overriding the default log instance.
func InitLogger(log Logger) {
DefaultLogger = log
}
// WithContext returns a shallow copy of l with its context changed
// to ctx. The provided ctx must be non-nil.
func WithContext(ctx context.Context) Logger {
return DefaultLogger.WithContext(ctx)
}
// SetLevel set logger level
func SetLevel(lv Level) {
DefaultLogger.SetLevel(lv)
}
// Debug uses fmt.Sprint to construct and log a message.
func Debug(args ...interface{}) {
DefaultLogger.WithCallDepth(1).Debug(args...)
}
// Debugf uses fmt.Sprintf to log a templated message.
func Debugf(format string, args ...interface{}) {
DefaultLogger.WithCallDepth(1).Debugf(format, args...)
}
// Debugw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
//
// When debug-level logging is disabled, this is much faster than
// s.With(keysAndValues).Debug(msg)
func Debugw(msg string, keysAndValues ...interface{}) {
DefaultLogger.WithCallDepth(1).Debugw(msg, keysAndValues...)
}
// Info uses fmt.Sprint to construct and log a message.
func Info(args ...interface{}) {
DefaultLogger.WithCallDepth(1).Info(args...)
}
// Infof uses fmt.Sprintf to log a templated message.
func Infof(format string, args ...interface{}) {
DefaultLogger.WithCallDepth(1).Infof(format, args...)
}
// Infow logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func Infow(msg string, keysAndValues ...interface{}) {
DefaultLogger.WithCallDepth(1).Infow(msg, keysAndValues...)
}
// Warn uses fmt.Sprint to construct and log a message.
func Warn(args ...interface{}) {
DefaultLogger.WithCallDepth(1).Warn(args...)
}
// Warnf uses fmt.Sprintf to log a templated message.
func Warnf(format string, args ...interface{}) {
DefaultLogger.WithCallDepth(1).Warnf(format, args...)
}
// Warnw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func Warnw(msg string, keysAndValues ...interface{}) {
DefaultLogger.WithCallDepth(1).Warnw(msg, keysAndValues...)
}
// Error uses fmt.Sprint to construct and log a message.
func Error(args ...interface{}) {
DefaultLogger.WithCallDepth(1).Error(args...)
}
// Errorf uses fmt.Sprintf to log a templated message.
func Errorf(format string, args ...interface{}) {
DefaultLogger.WithCallDepth(1).Errorf(format, args...)
}
// Errorw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func Errorw(msg string, keysAndValues ...interface{}) {
DefaultLogger.WithCallDepth(1).Errorw(msg, keysAndValues...)
}
// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
// Deprecated: 记录消息后,直接调用 os.Exit(1),这意味着:
// 在其他 goroutine defer 语句不会被执行;
// 各种 buffers 不会被 flush,包括日志的;
// 临时文件或者目录不会被移除;
// 不要使用 fatal 记录日志,而是向调用者返回错误。如果错误一直持续到 main.main。main.main 那就是在退出之前做处理任何清理操作的正确位置。
func Fatal(args ...interface{}) {
DefaultLogger.WithCallDepth(1).Fatal(args...)
}
// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
// Deprecated: 记录消息后,直接调用 os.Exit(1),这意味着:
// 在其他 goroutine defer 语句不会被执行;
// 各种 buffers 不会被 flush,包括日志的;
// 临时文件或者目录不会被移除;
// 不要使用 fatal 记录日志,而是向调用者返回错误。如果错误一直持续到 main.main。main.main 那就是在退出之前做处理任何清理操作的正确位置。
func Fatalf(format string, args ...interface{}) {
DefaultLogger.WithCallDepth(1).Fatalf(format, args...)
}
// Fatalw logs a message with some additional context, then calls os.Exit. The
// variadic key-value pairs are treated as they are in With.
// Deprecated: 记录消息后,直接调用 os.Exit(1),这意味着:
// 在其他 goroutine defer 语句不会被执行;
// 各种 buffers 不会被 flush,包括日志的;
// 临时文件或者目录不会被移除;
// 不要使用 fatal 记录日志,而是向调用者返回错误。如果错误一直持续到 main.main。main.main 那就是在退出之前做处理任何清理操作的正确位置。
func Fatalw(msg string, keysAndValues ...interface{}) {
DefaultLogger.WithCallDepth(1).Fatalw(msg, keysAndValues...)
}
// Sync flushes any buffered log entries.
func Sync() error {
return DefaultLogger.Sync()
}