/
log_cheerlog.go
175 lines (119 loc) · 4.07 KB
/
log_cheerlog.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
package cheerlib
import (
"fmt"
rotatelogs "github.com/lestrrat/go-file-rotatelogs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"time"
)
type CheerLog struct {
mLogger *zap.Logger
}
func NewCheerLogOnFile() *CheerLog {
var xError error = nil
var xThis *CheerLog = nil
xThis = new(CheerLog)
xError, xInfoLogWriter := xThis.getLogFileWriter(fmt.Sprintf("%s_info", ApplicationFileName()))
if xError != nil {
StdError(fmt.Sprintf("NewCheerLog CheerLog.getLogWriter Error=[%s]", xError.Error()))
}
xError, xWarnLogWriter := xThis.getLogFileWriter(fmt.Sprintf("%s_warn", ApplicationFileName()))
if xError != nil {
StdError(fmt.Sprintf("NewCheerLog CheerLog.getLogWriter Error=[%s]", xError.Error()))
}
xError, xErrorLogWriter := xThis.getLogFileWriter(fmt.Sprintf("%s_error", ApplicationFileName()))
if xError != nil {
StdError(fmt.Sprintf("NewCheerLog CheerLog.getLogWriter Error=[%s]", xError.Error()))
}
xError = xThis.InitWithLogWriter(xInfoLogWriter,xWarnLogWriter, xErrorLogWriter)
if xError != nil {
StdError(fmt.Sprintf("NewCheerLog CheerLog.InitWithLogWriter Error=[%s]", xError.Error()))
}
return xThis
}
func (this *CheerLog) LogInfo(logContent string) {
if this.mLogger == nil {
return
}
this.mLogger.Info(logContent)
}
func (this *CheerLog) LogWarn(logContent string) {
if this.mLogger == nil {
return
}
this.mLogger.Warn(logContent)
}
func (this *CheerLog) LogError(logContent string) {
if this.mLogger == nil {
return
}
this.mLogger.Error(logContent)
}
func (this *CheerLog) InitWithLogWriter(logWriterForInfo zapcore.WriteSyncer,logWriterForWarn zapcore.WriteSyncer, logWriterForError zapcore.WriteSyncer) error {
var xError error = nil
xEncoder := this.getLogEncoder()
xLogInfoLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.InfoLevel
})
xLogWarnLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.WarnLevel
})
xLogErrorLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})
xLogCore := zapcore.NewTee(
zapcore.NewCore(xEncoder, logWriterForInfo, xLogInfoLevel),
zapcore.NewCore(xEncoder, logWriterForWarn, xLogWarnLevel),
zapcore.NewCore(xEncoder, logWriterForError, xLogErrorLevel),
)
this.mLogger = zap.New(xLogCore,
zap.AddCaller(),
zap.AddCallerSkip(2),
)
// 替换全局ZAP
if this.mLogger != nil {
zap.ReplaceGlobals(this.mLogger)
}
return xError
}
func (this *CheerLog) getEncodeLevel(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString("[" + level.CapitalString() + "]")
}
func (this *CheerLog) getLogEncoder() zapcore.Encoder {
xConfig := zap.NewProductionEncoderConfig()
xConfig.EncodeTime = zapcore.ISO8601TimeEncoder
xConfig.EncodeLevel = this.getEncodeLevel
xEncoder := zapcore.NewConsoleEncoder(xConfig)
return xEncoder
}
func (this *CheerLog) getLogFileWriter(logFileName string) (error, zapcore.WriteSyncer) {
var xError error = nil
xLogFilePath := fmt.Sprintf("%s/log", ApplicationBaseDirectory())
if !DirectoryExists(xLogFilePath) {
DirectoryCreateDirectory(xLogFilePath)
}
xLogFilePath = fmt.Sprintf("%s/%s", xLogFilePath, logFileName)
xLogFileWriter, xError := rotatelogs.New(
xLogFilePath+"_%Y%m%d%H.log", //日志命名规则
rotatelogs.WithLinkName(xLogFilePath+".log"), //生成软链,指向最新日志文件
rotatelogs.WithRotationTime(time.Minute), //最小为1分钟轮询。默认60s 低于1分钟就按1分钟来
rotatelogs.WithMaxAge(gCheerLogFileMaxAge), //保留时间
rotatelogs.WithClock(rotatelogs.Local), //本地时区
)
if xError!=nil{
return xError, nil
}
xWriteSyncer := zapcore.AddSync(xLogFileWriter)
return xError, xWriteSyncer
}
func (this *CheerLog) getStdErrorWriter() (error, zapcore.WriteSyncer) {
var xError error = nil
xWriteSyncer := zapcore.AddSync(os.Stderr)
return xError, xWriteSyncer
}
func (this *CheerLog) getStdOutWriter() (error, zapcore.WriteSyncer) {
var xError error = nil
xWriteSyncer := zapcore.AddSync(os.Stdout)
return xError, xWriteSyncer
}