-
Notifications
You must be signed in to change notification settings - Fork 0
/
zaplog.go
189 lines (163 loc) · 4.21 KB
/
zaplog.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
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
package logger
import (
"os"
"os/signal"
"syscall"
)
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
// this zap logger refer to dubbo-go logger.go
type Logger interface {
Debug(args ...interface{})
Info(args ...interface{})
Warn(args ...interface{})
Error(args ...interface{})
DPanic(args ...interface{})
Panic(args ...interface{})
Fatal(args ...interface{})
Debugf(fmt string, args ...interface{})
Infof(fmt string, args ...interface{})
Warnf(fmt string, args ...interface{})
Errorf(fmt string, args ...interface{})
DPanicf(fmt string, args ...interface{})
Panicf(fmt string, args ...interface{})
Fatalf(fmt string, args ...interface{})
}
type EnvLogger string
type FileLogger struct {
Filename string
MaxSize int
MaxAge int
MaxBackups int
LocalTime bool
Compress bool
}
const (
LogReleaseEnv EnvLogger = "release"
LogDevelopEnv EnvLogger = "develop"
)
var (
log Logger
zapLogger *zap.Logger
zapLoggerConfig = zap.NewDevelopmentConfig()
zapLoggerEncoderConfig = zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "message",
StacktraceKey: "stacktrace",
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
)
func init() {
zapLoggerConfig.EncoderConfig = zapLoggerEncoderConfig
zapLogger, _ = zapLoggerConfig.Build()
log = zapLogger.Sugar()
// flushes buffer when redirect log to file.
var exitSignal = make(chan os.Signal)
signal.Notify(exitSignal, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-exitSignal
// Sync calls the underlying Core's Sync method, flushing any buffered log entries.
// Applications should take care to call Sync before exiting.
err := zapLogger.Sync() // flushes buffer, if any
if err != nil {
log.Infof("zapLogger sync err: %s", err.Error())
}
os.Exit(0)
}()
}
type Options struct {
appName string
env EnvLogger
level zapcore.Level
callerSkip int
fileLog *FileLogger
}
type Option func(*Options)
const (
DefaultEnv = LogDevelopEnv
DefaultLevel = zapcore.DebugLevel
DefaultCallerSkip = 1
)
func Env(env EnvLogger) Option {
return func(o *Options) {
o.env = env
}
}
func Level(level zapcore.Level) Option {
return func(o *Options) {
o.level = level
}
}
func CallerSkip(callerSkip int) Option {
return func(o *Options) {
o.callerSkip = callerSkip
}
}
func FileLog(fileLog *FileLogger) Option {
return func(o *Options) {
o.fileLog = fileLog
}
}
// SetLogger customize yourself logger.
func SetLogger(options ...Option) (err error) {
opts := Options{
env: DefaultEnv,
level: DefaultLevel,
callerSkip: DefaultCallerSkip,
}
for _, o := range options {
o(&opts)
}
if opts.fileLog != nil {
core := zapcore.NewCore(
zapcore.NewJSONEncoder(zapLoggerEncoderConfig),
zapcore.AddSync(&lumberjack.Logger{
Filename: opts.fileLog.Filename,
MaxSize: opts.fileLog.MaxSize,
MaxAge: opts.fileLog.MaxAge,
MaxBackups: opts.fileLog.MaxBackups,
LocalTime: opts.fileLog.LocalTime,
Compress: opts.fileLog.Compress,
}),
zap.NewAtomicLevelAt(opts.level),
)
zapLogger = zap.New(core, zap.AddCaller(), zap.AddCallerSkip(opts.callerSkip))
} else {
if opts.env == LogReleaseEnv {
zapLoggerConfig = zap.NewProductionConfig()
zapLoggerConfig.EncoderConfig = zapLoggerEncoderConfig
}
zapLoggerConfig.Level = zap.NewAtomicLevelAt(opts.level)
zapLogger, err = zapLoggerConfig.Build(zap.AddCaller(), zap.AddCallerSkip(opts.callerSkip))
if err != nil {
return err
}
}
log = zapLogger.Sugar()
return nil
}
// GetLogger get logger
func GetLogger() Logger {
return log
}
// SetLoggerCallerDisable disable caller info in production env for performance improve.
// It is highly recommended that you execute this method in a production environment.
func SetLoggerCallerDisable() (err error) {
zapLoggerConfig.Development = false
zapLoggerConfig.DisableCaller = true
zapLogger, err = zapLoggerConfig.Build()
if err != nil {
return err
}
log = zapLogger.Sugar()
return nil
}