-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
108 lines (90 loc) · 2.51 KB
/
options.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
package logger
import (
"go.uber.org/zap"
)
// Options log config option.
type Options struct {
Level string
DisableCaller bool
Fields []zap.Field
Format string
//Development bool
DisableStacktrace bool
OutputPaths []string
ErrorOutputPaths []string
EnableColor bool
Name string
}
type Option func(*Options)
// WithDisableStacktrace change log Stacktrace status.
func WithDisableStacktrace(enable bool) Option {
return func(o *Options) {
o.DisableStacktrace = enable
}
}
//// WithDevelopment change log development status.
//func WithDevelopment(enable bool) Option {
// return func(o *Options) {
// o.Development = enable
// }
//}
// WithLevel change log level, default info.
func WithLevel(level string) Option {
return func(o *Options) {
o.Level = level
}
}
// WithEnableColor change log enable color.
func WithEnableColor(enable bool) Option {
return func(o *Options) {
o.EnableColor = enable
}
}
// WithFields append logger fields.
func WithFields(fields ...zap.Field) Option {
//zap.Fields() math return zapcore.Field
//zap.Fields()
return func(o *Options) {
o.Fields = append(o.Fields, fields...)
}
}
// WithFormat log format.
func WithFormat(format string) Option {
return func(o *Options) {
o.Format = format
}
}
// WithOutputPaths change output object.
func WithOutputPaths(outputPaths []string) Option {
return func(o *Options) {
o.OutputPaths = outputPaths
}
}
// WithErrorOutputPaths change error output object.
func WithErrorOutputPaths(errorOutputPaths []string) Option {
/*
ErrorOutputPaths 在 zap 配置中用于指定错误输出的路径。
它主要用于记录 zap 日志库内部产生的错误,例如编码错误或写入日志时的失败。
这与应用程序使用 logger.Error(...) 记录的错误日志不同。ErrorOutputPaths 更关注于日志系统内部的错误,而不是应用程序逻辑中产生的错误。
*/
return func(o *Options) {
o.ErrorOutputPaths = errorOutputPaths
}
}
// WithDisableCaller manage caller status.
func WithDisableCaller(caller bool) Option {
return func(o *Options) {
o.DisableCaller = caller
}
}
// WithValues creates a child logger and adds Zap Fields to it.
func WithValues(keysAndValues ...zap.Field) *zap.Logger {
return Log.WithValues(keysAndValues...)
}
func (l *log) WithValues(keysAndValues ...zap.Field) *zap.Logger {
//newLogger := l.Sugar().With(zap.Fields(keysAndValues...))
if len(keysAndValues) == 0 {
return l.Logger
}
return l.With(keysAndValues...)
}