-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
238 lines (211 loc) · 6.52 KB
/
logger.go
File metadata and controls
238 lines (211 loc) · 6.52 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package logger
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Log wrap zap logger
type Log struct {
log *zap.Logger
level AtomicLevel
hooks []Hook
// for caller
callerCore *CallerCore
}
// NewLoggerWith new logger with zap logger and atomic level
func NewLoggerWith(logger *zap.Logger, lv AtomicLevel) *Log {
return &Log{
log: logger,
level: lv,
hooks: nil,
callerCore: NewCallerCore(),
}
}
// NewLogger new logger
// 默认配置:
//
// Level: 日志等级, 默认warn
// Format: 编码格式, 默认json
// EncodeLevel: 编码器类型, 默认LowercaseLevelEncoder
// Adapter: 默认输出适合器, 默认console`
// Stack: 是否使能栈调试输出, 默认false
// Path: 日志保存路径, 默认当前路径
// Writer: 当adapter有附带custom时, 如果为writer为空, 将使用os.Stdout
// EncoderConfig: 如果配置该项,则 EncodeLevel 将被覆盖
//
// 文件日志切割配置(启用file时生效)
// Filename 空字符使用默认, 默认<processname>-lumberjack.log
// MaxSize 每个日志文件最大尺寸(MB), 默认100MB
// MaxAge 日志文件保存天数, 默认0 不删除
// MaxBackups 日志文件保存备份数, 默认0 都保存
// LocalTime 是否格式化时间戳, 默认UTC时间
// Compress 是否使用gzip压缩文件, 采用默认不压缩
//
// Caller相关
// callerLevel caller日志级别, 默认warn
// callerSkip caller设置跳过深度, 默认0
// callerSkipPackages caller设置跳过的包名, 默认空
func NewLogger(opts ...Option) *Log { return NewLoggerWith(New(opts...)) }
// SetNewCallerCore overwrite with new caller core
func (l *Log) SetNewCallerCore(c *CallerCore) *Log {
if c != nil {
l.callerCore = c
}
return l
}
// AddCallerSkip add the number of callers skipped by caller annotation.
func (l *Log) AddCallerSkip(callerSkip int) *Log {
l.callerCore.AddSkip(callerSkip)
return l
}
// AddCallerSkipPackage add the caller skip package.
func (l *Log) AddCallerSkipPackage(vs ...string) *Log {
l.callerCore.AddSkipPackage(vs...)
return l
}
// SetCallerLevel set the caller level.
func (l *Log) SetCallerLevel(lv Level) *Log {
l.callerCore.SetLevel(lv)
return l
}
// UseExternalCallerLevel use external caller level, which controller by user.
func (l *Log) UseExternalCallerLevel(lvl AtomicLevel) *Log {
l.callerCore.UseExternalLevel(lvl)
return l
}
// UnderlyingCallerLevel return underlying caller level.
func (l *Log) UnderlyingCallerLevel() AtomicLevel {
return l.callerCore.UnderlyingLevel()
}
// SetCaller set the caller function.
func (l *Log) SetCaller(f func(depth int, skipPackages ...string) Field) *Log {
if f != nil {
l.callerCore.Caller = f
}
return l
}
// SetLevelWithText alters the logging level.
// ParseAtomicLevel set the logging level based on a lowercase or all-caps ASCII
// representation of the log level.
// If the provided ASCII representation is
// invalid an error is returned.
// see zapcore.Level
func (l *Log) SetLevelWithText(text string) error {
lv, err := zapcore.ParseLevel(text)
if err != nil {
return err
}
l.level.SetLevel(lv)
return nil
}
// SetLevel alters the logging level.
func (l *Log) SetLevel(lv Level) *Log {
l.level.SetLevel(lv)
return l
}
// ExtendDefaultHook set default hook, which hold always until you call [Event.Msg]/[Event.Print]/[Event.Printf].
func (l *Log) ExtendDefaultHook(hs ...Hook) *Log {
hooks := make([]Hook, len(l.hooks)+len(hs))
copy(hooks, l.hooks)
copy(hooks[len(l.hooks):], hs)
l.hooks = hooks
return l
}
// ExtendDefaultHookFunc set default hook, which hold always until you call [Event.Msg]/[Event.Print]/[Event.Printf].
func (l *Log) ExtendDefaultHookFunc(hs ...HookFunc) *Log {
hooks := make([]Hook, len(l.hooks)+len(hs))
copy(hooks, l.hooks)
for i := range hs {
hooks[len(l.hooks)+i] = hs[i]
}
l.hooks = hooks
return l
}
// GetLevel returns the minimum enabled log level.
func (l *Log) GetLevel() Level { return l.level.Level() }
// Enabled returns true if the given level is at or above this level.
func (l *Log) Enabled(lvl Level) bool { return l.level.Enabled(lvl) }
// V returns true if the given level is at or above this level.
// same as Enabled
func (l *Log) V(lvl Level) bool { return l.level.Enabled(lvl) }
// Sugar wraps the Logger to provide a more ergonomic, but slightly slower,
// API. Sugaring a Logger is quite inexpensive, so it's reasonable for a
// single application to use both Loggers and SugaredLoggers, converting
// between them on the boundaries of performance-sensitive code.
func (l *Log) Sugar() *zap.SugaredLogger { return l.log.Sugar() }
// Logger return internal [zap.Logger]
func (l *Log) Logger() *zap.Logger { return l.log }
// ExtendHook creates a child log with extend Hook.
func (l *Log) ExtendHook(hs ...Hook) *Log {
hooks := make([]Hook, len(l.hooks)+len(hs))
copy(hooks, l.hooks)
copy(hooks[len(l.hooks):], hs)
return &Log{
log: l.log,
level: l.level,
hooks: hooks,
callerCore: l.callerCore,
}
}
// ExtendHookFunc creates a child log with extend Hook.
func (l *Log) ExtendHookFunc(hs ...HookFunc) *Log {
hooks := make([]Hook, len(l.hooks)+len(hs))
copy(hooks, l.hooks)
for i := range hs {
hooks[len(l.hooks)+i] = hs[i]
}
return &Log{
log: l.log,
level: l.level,
hooks: hooks,
callerCore: l.callerCore,
}
}
// WithNewHook creates a child log with new hook without default hook.
func (l *Log) WithNewHook(hs ...Hook) *Log {
hooks := make([]Hook, len(hs))
copy(hooks, hs)
return &Log{
log: l.log,
level: l.level,
hooks: hooks,
callerCore: l.callerCore,
}
}
// WithNewHookFunc creates a child log with new hook func without default hook.
func (l *Log) WithNewHookFunc(hs ...HookFunc) *Log {
hooks := make([]Hook, len(hs))
for i := range hs {
hooks[i] = hs[i]
}
return &Log{
log: l.log,
level: l.level,
hooks: hooks,
callerCore: l.callerCore,
}
}
// With creates a child log and adds structured context to it. Fields added
// to the child don't affect the parent, and vice versa.
//
// NOTICE: if you do not need a child log, use [Event.With] instead.
func (l *Log) With(fields ...Field) *Log {
return &Log{
log: l.log.With(fields...),
level: l.level,
hooks: l.hooks,
callerCore: l.callerCore,
}
}
// Named adds a sub-scope to the logger's name. See [Logger.Named] for details.
func (l *Log) Named(name string) *Log {
return &Log{
log: l.log.Named(name),
level: l.level,
hooks: l.hooks,
callerCore: l.callerCore,
}
}
// Sync flushes any buffered log entries.
func (l *Log) Sync() error {
return l.log.Sync()
}