-
Notifications
You must be signed in to change notification settings - Fork 0
/
zlogger.go
120 lines (105 loc) · 3.18 KB
/
zlogger.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
package zlogger
import (
"fmt"
"os"
"strings"
"sync"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Logger struct {
Drivers sync.Map
Options
}
type Options struct {
Mode string `json:"mode"`
LogPath string `json:"log_path" yaml:"log_path"`
FileName string `json:"file_name"`
MaxSize int `json:"max_size"`
MaxAge int `json:"max_age"`
MaxBackups int `json:"max_backups"`
}
func NewLogger(options Options) *Logger {
l := &Logger{Options: options}
l.Drivers.Store("default", l.getDefaultDriver())
return l
}
// Store 使用定制日志文件名
func (l *Logger) Store(fileName string) *zap.Logger {
if fileName == "" {
if load, ok := l.Drivers.Load("default"); ok {
return load.(*zap.Logger)
}
panic("l.Drivers.Load Error")
}
store, _ := l.Drivers.LoadOrStore(fileName, l.setDriver(fileName))
return store.(*zap.Logger)
}
// Default 默认日志类
func (l *Logger) Default() *zap.Logger {
load, _ := l.Drivers.Load("default")
return load.(*zap.Logger)
}
// getDefaultDriver 获取默认logDriver
func (l *Logger) getDefaultDriver() *zap.Logger {
fileName := "app"
var path = l.LogPath
if ok := strings.HasSuffix(l.LogPath, "/"); ok {
path = l.LogPath[0 : len(l.LogPath)-1]
}
if l.FileName != "" {
fileName = l.FileName
}
defaultWriter := getLogWriter(fmt.Sprintf("%s/%s.log", path, fileName), l.MaxSize, l.MaxBackups, l.MaxAge)
defaultEncoder := getEncoder()
writeSyncer := zapcore.NewMultiWriteSyncer(defaultWriter)
if l.Options.Mode == "dev" {
writeSyncer = zapcore.NewMultiWriteSyncer(defaultWriter, zapcore.AddSync(os.Stdout))
}
core := zapcore.NewCore(defaultEncoder, writeSyncer, zapcore.DebugLevel)
return zap.New(core, zap.AddCaller())
}
// setDriver 设置新的logDriver
func (l *Logger) setDriver(name string) *zap.Logger {
var path = l.LogPath
if ok := strings.HasSuffix(l.LogPath, "/"); ok {
path = l.LogPath[0 : len(l.LogPath)-1]
}
driverWriter := getLogWriter(fmt.Sprintf("%s/%s.log", path, name), l.MaxSize, l.MaxBackups, l.MaxAge)
driverEncoder := getEncoder()
writeSyncer := zapcore.NewMultiWriteSyncer(driverWriter)
if l.Options.Mode == "dev" {
writeSyncer = zapcore.NewMultiWriteSyncer(driverWriter, zapcore.AddSync(os.Stdout))
}
core := zapcore.NewCore(driverEncoder, writeSyncer, zap.DebugLevel)
return zap.New(core, zap.AddCaller())
}
// Sync 退出时将所有drivers中的日志信息刷出缓冲区
func (l *Logger) Sync() {
l.Drivers.Range(func(key, value interface{}) bool {
if key == nil {
return false
}
_ = value.(*zap.Logger).Sync()
return true
})
}
func getEncoder() zapcore.Encoder {
config := zap.NewProductionEncoderConfig()
config.EncodeTime = zapcore.ISO8601TimeEncoder
config.TimeKey = "time"
config.EncodeLevel = zapcore.CapitalLevelEncoder
config.EncodeDuration = zapcore.SecondsDurationEncoder
config.EncodeCaller = zapcore.ShortCallerEncoder
return zapcore.NewJSONEncoder(config)
}
func getLogWriter(filename string, maxSize, maxBackup, maxAge int) zapcore.WriteSyncer {
lumberJackLogger := &lumberjack.Logger{
Filename: filename,
MaxSize: maxSize,
MaxBackups: maxBackup,
MaxAge: maxAge,
}
return zapcore.AddSync(lumberJackLogger)
}