/
tee_core.go
84 lines (64 loc) · 1.59 KB
/
tee_core.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
package log
import (
"os"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
/*
Filename: filepath + filename (the place file stored)
MaxSize:the max mememory of a log file(MB)
MaxBackups:the max number of log files stored
MaxAges:the latest time stored old log files
Compress:whether to compress and archive old files
*/
type RotateOptions struct {
MaxSize int
MaxBackups int
MaxAge int
Compress bool
}
type LevelEnablerFunc = zap.LevelEnablerFunc
type TeeOption struct {
Filename string
Ropt RotateOptions
Lef LevelEnablerFunc
}
func newTeeWithRotateCore(tops []TeeOption) zapcore.Core {
cores := []zapcore.Core{}
cfg := zap.NewProductionConfig()
cfg.EncoderConfig.EncodeTime = timeEncoder // add time format
for _, top := range tops {
lv := top.Lef
w := &lumberjack.Logger{
Filename: top.Filename,
MaxSize: top.Ropt.MaxSize,
MaxBackups: top.Ropt.MaxBackups,
MaxAge: top.Ropt.MaxAge,
Compress: top.Ropt.Compress,
}
core := zapcore.NewCore(
zapcore.NewJSONEncoder(cfg.EncoderConfig),
zapcore.AddSync(w),
lv,
)
cores = append(cores, core)
}
core := zapcore.NewTee(cores...)
return core
}
func newTeeWithRotate(tops []TeeOption, level Level, opts ...Option) *Logger {
core := newTeeWithRotateCore(tops)
// if it is debug level, then I will start stderr output.
if level == DebugLevel {
stdCore := newCore(os.Stderr, DebugLevel)
core = zapcore.NewTee(core, stdCore)
}
log := zap.New(core, opts...)
suger := log.Sugar()
logger := &Logger{
l: log,
s: suger,
}
return logger
}