-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
137 lines (118 loc) · 2.96 KB
/
log.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
package log
import (
"context"
"os"
"sync"
"github.com/xyser/dnsx/pkg/config"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
var once sync.Once
// TraceID trace id
const TraceID = "trace_id"
// levelType log level
var levelType = map[string]zapcore.Level{
"debug": zap.DebugLevel,
"info": zap.InfoLevel,
"warn": zap.WarnLevel,
"error": zap.ErrorLevel,
}
// Logger logger 标准日志
type Logger struct {
*zap.Logger
}
var logger = new(Logger)
// New Logger new Logger
func New() *Logger {
return logger
}
// level 日志级别操作
var level = zap.NewAtomicLevel()
// Init Setup init Logger
func Init() {
once.Do(func() {
SetLevel(config.GetString("log.log_level"))
core := zapcore.NewCore(
encoder(),
zapcore.NewMultiWriteSyncer(writers()...),
level,
)
logger.Logger = zap.New(core, zap.AddCaller(), zap.Development()).
With(zap.String("app_name", config.GetString("app.name")))
// 注册配置变更事件
config.RegisterChangeEvent(func(e fsnotify.Event) {
SetLevel(config.GetString("log.log_level"))
})
})
}
// WithContext 从上下文中获取 trace-id 并在日志中加入 trace-id 字段
func (l Logger) WithContext(c context.Context) Logger {
id, ok := c.Value(TraceID).(string)
if !ok {
id = ""
}
l.Logger = l.With(zap.String(TraceID, id))
return l
}
// SetLevel 设置日志级别
func SetLevel(name string) {
var l zapcore.Level
if v, ok := levelType[name]; ok {
l = v
} else {
l = zap.InfoLevel
}
if l == GetLevel() {
return
}
level.SetLevel(l)
}
// GetLevel 获取当前日志级别
func GetLevel() zapcore.Level {
return level.Level()
}
// getLogfilePath 获取日志文件全路径
func getLogfilePath() string {
return config.GetString("log.log_path") + config.GetString("log.log_file_name") + ".log"
}
// writers 日志输出
func writers() (ws []zapcore.WriteSyncer) {
handle := lumberjack.Logger{
Filename: getLogfilePath(),
MaxSize: viper.GetInt("log.max_size"),
MaxBackups: viper.GetInt("log.max_backups"),
MaxAge: viper.GetInt("log.max_age"),
Compress: true,
}
ws = []zapcore.WriteSyncer{
zapcore.AddSync(&handle),
}
if viper.GetBool("log.stdout") {
ws = append(ws, zapcore.AddSync(os.Stdout))
}
return
}
// encoder 日志编码
func encoder() zapcore.Encoder {
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "category",
CallerKey: "line",
MessageKey: "msg",
StacktraceKey: "stack",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.FullCallerEncoder,
EncodeName: zapcore.FullNameEncoder,
}
if config.GetString("log.encoder") == "json" {
return zapcore.NewJSONEncoder(encoderConfig)
}
return zapcore.NewConsoleEncoder(encoderConfig)
}