forked from jacexh/ultron
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlog.go
40 lines (33 loc) · 823 Bytes
/
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
package ultron
import (
"sync"
"github.com/jacexh/gopkg/zaprotate"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
// Logger 全局日志
Logger *zap.Logger
levelMapper = map[string]zapcore.Level{
"info": zapcore.InfoLevel,
"debug": zapcore.DebugLevel,
"warn": zapcore.WarnLevel,
"error": zapcore.ErrorLevel,
}
once sync.Once
)
func buildLogger(opt LoggerOption) {
once.Do(func() {
cfg := zap.NewProductionConfig()
cfg.EncoderConfig.TimeKey = "@timestamp"
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
cfg.Sampling = nil
cfg.Level = zap.NewAtomicLevelAt(levelMapper[opt.Level])
Logger = zaprotate.BuildRotateLogger(cfg, zaprotate.RotatingFileConfig{
LoggerName: "",
Filename: opt.FileName,
MaxSize: opt.MaxSize,
MaxBackups: opt.MaxBackups,
})
})
}