-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzap.go
More file actions
176 lines (160 loc) · 4.89 KB
/
zap.go
File metadata and controls
176 lines (160 loc) · 4.89 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
package logger
import (
"os"
"path/filepath"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
type AtomicLevel = zap.AtomicLevel
type Field = zap.Field
type Level = zapcore.Level
type ObjectMarshaler = zapcore.ObjectMarshaler
type ArrayMarshaler = zapcore.ArrayMarshaler
type ObjectEncoder = zapcore.ObjectEncoder
type ArrayEncoder = zapcore.ArrayEncoder
// log level defined
const (
DebugLevel = zap.DebugLevel
InfoLevel = zap.InfoLevel
WarnLevel = zap.WarnLevel
ErrorLevel = zap.ErrorLevel
DPanicLevel = zap.DPanicLevel
PanicLevel = zap.PanicLevel
FatalLevel = zap.FatalLevel
)
// adapter defined
const (
AdapterConsole = "console" // console
AdapterFile = "file" // file
AdapterMulti = "multi" // file and console
AdapterCustom = "custom" // custom io.Writer
AdapterConsoleCustom = "console-custom" // console and custom io.Writer
AdapterFileCustom = "file-custom" // file and custom io.Writer
AdapterMultiCustom = "multi-custom" // file, console and custom io.Writer
)
// format defined
const (
FormatJson = "json"
FormatConsole = "console"
)
// encode level defined
const (
EncodeLevelLowercase = "LowercaseLevelEncoder" // 小写编码器
EncodeLevelLowercaseColor = "LowercaseColorLevelEncoder" // 小写编码器带颜色
EncodeLevelCapital = "CapitalLevelEncoder" // 大写编码器
EncodeLevelCapitalColor = "CapitalColorLevelEncoder" // 大写编码器带颜色
)
// New constructs a new Log
func New(opts ...Option) (*zap.Logger, AtomicLevel) {
c := &Config{}
for _, opt := range opts {
opt(c)
}
var options []zap.Option
if c.Stack {
// 栈调用,及使能等级
options = append(options, zap.AddStacktrace(zap.NewAtomicLevelAt(zap.DPanicLevel))) // 只显示栈的错误等级
}
level, err := zap.ParseAtomicLevel(c.Level)
if err != nil {
level = zap.NewAtomicLevelAt(zap.WarnLevel)
}
// 初始化core
core := zapcore.NewCore(
toEncoder(c, level), // 设置encoder
toWriter(c), // 设置输出
level, // 设置日志输出等级
)
return zap.New(core, options...), level
}
func toEncoder(c *Config, level AtomicLevel) zapcore.Encoder {
encoderConfig := c.EncoderConfig
if encoderConfig == nil {
encoderConfig = &zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: toEncodeLevel(c.EncodeLevel),
EncodeTime: zapcore.RFC3339TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
if level.Level() == DebugLevel {
encoderConfig.EncodeCaller = zapcore.FullCallerEncoder
}
}
if c.Format == FormatConsole {
return zapcore.NewConsoleEncoder(*encoderConfig)
}
return zapcore.NewJSONEncoder(*encoderConfig)
}
func toEncodeLevel(l string) zapcore.LevelEncoder {
switch l {
case EncodeLevelLowercaseColor: // 小写编码器带颜色
return zapcore.LowercaseColorLevelEncoder
case EncodeLevelCapital: // 大写编码器
return zapcore.CapitalLevelEncoder
case EncodeLevelCapitalColor: // 大写编码器带颜色
return zapcore.CapitalColorLevelEncoder
case EncodeLevelLowercase: // 小写编码器(默认)
fallthrough
default:
return zapcore.LowercaseLevelEncoder
}
}
func toWriter(c *Config) zapcore.WriteSyncer {
cf := c.File
fileWriter := func() zapcore.WriteSyncer {
return zapcore.AddSync(&lumberjack.Logger{ // 文件切割
Filename: filepath.Join(cf.Path, cf.Filename),
MaxSize: cf.MaxSize,
MaxAge: cf.MaxAge,
MaxBackups: cf.MaxBackups,
LocalTime: cf.LocalTime,
Compress: cf.Compress,
})
}
stdoutWriter := func() zapcore.WriteSyncer {
return zapcore.AddSync(os.Stdout)
}
customWriter := func(w ...zapcore.WriteSyncer) []zapcore.WriteSyncer {
ws := make([]zapcore.WriteSyncer, 0, len(c.Writer)+len(w))
for _, writer := range c.Writer {
ws = append(ws, zapcore.AddSync(writer))
}
for _, writer := range w {
ws = append(ws, zapcore.AddSync(writer))
}
return ws
}
switch strings.ToLower(c.Adapter) {
case AdapterFile:
return fileWriter()
case AdapterMulti:
return zapcore.NewMultiWriteSyncer(stdoutWriter(), fileWriter())
case AdapterCustom:
ws := customWriter()
if len(ws) == 0 {
return stdoutWriter()
}
if len(ws) == 1 {
return ws[0]
}
return zapcore.NewMultiWriteSyncer(ws...)
case AdapterFileCustom:
return zapcore.NewMultiWriteSyncer(customWriter(fileWriter())...)
case AdapterConsoleCustom:
return zapcore.NewMultiWriteSyncer(customWriter(stdoutWriter())...)
case AdapterMultiCustom:
return zapcore.NewMultiWriteSyncer(customWriter(stdoutWriter(), fileWriter())...)
default: // console
return stdoutWriter()
}
}