-
Notifications
You must be signed in to change notification settings - Fork 4
/
customize_output.go
72 lines (61 loc) · 1.61 KB
/
customize_output.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.
package logger
import (
"io"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func newCustomizeRootLogger(level string, options int, ws io.Writer) (*zap.Logger, error) {
// use lumberjack.Logger for rotate
w := zapcore.AddSync(ws)
cfg := zapcore.EncoderConfig{
NameKey: NameKeyMod,
MessageKey: NameKeyMsg,
LevelKey: NameKeyLevel,
TimeKey: NameKeyTime,
CallerKey: NameKeyPos,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeCaller: zapcore.FullCallerEncoder,
}
if options&OPT_COLOR != 0 {
cfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
if options&OPT_SHORT_CALLER != 0 {
cfg.EncodeCaller = zapcore.ShortCallerEncoder
}
var enc zapcore.Encoder
if options&OPT_ENC_CONSOLE != 0 {
enc = zapcore.NewConsoleEncoder(cfg)
} else {
enc = zapcore.NewJSONEncoder(cfg)
}
var lvl zapcore.Level
switch strings.ToLower(level) {
case INFO: // pass
lvl = zap.InfoLevel
case DEBUG:
lvl = zap.DebugLevel
case WARN:
lvl = zap.WarnLevel
case ERROR:
lvl = zap.ErrorLevel
case PANIC:
lvl = zap.PanicLevel
case DPANIC:
lvl = zap.DPanicLevel
case FATAL:
lvl = zap.FatalLevel
default:
lvl = zap.DebugLevel
}
core := zapcore.NewCore(enc, w, lvl)
// NOTE: why need add another option while
// zapcore.ShortCallerEncoder/FullCallerEncoder been set
l := zap.New(core, zap.AddCaller())
return l, nil
}