forked from trpc-group/trpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzaplogger.go
400 lines (357 loc) · 11.4 KB
/
zaplogger.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Tencent is pleased to support the open source community by making tRPC available.
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License that can be found in the LICENSE file.
package log
import (
"fmt"
"os"
"strconv"
"time"
"trpc.group/trpc-go/trpc-go/internal/report"
"trpc.group/trpc-go/trpc-go/log/rollwriter"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var defaultConfig = []OutputConfig{
{
Writer: "console",
Level: "debug",
Formatter: "console",
},
}
// Some ZapCore constants.
const (
ConsoleZapCore = "console"
FileZapCore = "file"
)
// Levels is the map from string to zapcore.Level.
var Levels = map[string]zapcore.Level{
"": zapcore.DebugLevel,
"trace": zapcore.DebugLevel,
"debug": zapcore.DebugLevel,
"info": zapcore.InfoLevel,
"warn": zapcore.WarnLevel,
"error": zapcore.ErrorLevel,
"fatal": zapcore.FatalLevel,
}
var levelToZapLevel = map[Level]zapcore.Level{
LevelTrace: zapcore.DebugLevel,
LevelDebug: zapcore.DebugLevel,
LevelInfo: zapcore.InfoLevel,
LevelWarn: zapcore.WarnLevel,
LevelError: zapcore.ErrorLevel,
LevelFatal: zapcore.FatalLevel,
}
var zapLevelToLevel = map[zapcore.Level]Level{
zapcore.DebugLevel: LevelDebug,
zapcore.InfoLevel: LevelInfo,
zapcore.WarnLevel: LevelWarn,
zapcore.ErrorLevel: LevelError,
zapcore.FatalLevel: LevelFatal,
}
// NewZapLog creates a trpc default Logger from zap whose caller skip is set to 2.
func NewZapLog(c Config) Logger {
return NewZapLogWithCallerSkip(c, 2)
}
// NewZapLogWithCallerSkip creates a trpc default Logger from zap.
func NewZapLogWithCallerSkip(cfg Config, callerSkip int) Logger {
var (
cores []zapcore.Core
levels []zap.AtomicLevel
)
for _, c := range cfg {
writer := GetWriter(c.Writer)
if writer == nil {
panic("log: writer core: " + c.Writer + " no registered")
}
decoder := &Decoder{OutputConfig: &c}
if err := writer.Setup(c.Writer, decoder); err != nil {
panic("log: writer core: " + c.Writer + " setup fail: " + err.Error())
}
cores = append(cores, decoder.Core)
levels = append(levels, decoder.ZapLevel)
}
return &zapLog{
levels: levels,
logger: zap.New(
zapcore.NewTee(cores...),
zap.AddCallerSkip(callerSkip),
zap.AddCaller(),
),
}
}
func newEncoder(c *OutputConfig) zapcore.Encoder {
encoderCfg := zapcore.EncoderConfig{
TimeKey: GetLogEncoderKey("T", c.FormatConfig.TimeKey),
LevelKey: GetLogEncoderKey("L", c.FormatConfig.LevelKey),
NameKey: GetLogEncoderKey("N", c.FormatConfig.NameKey),
CallerKey: GetLogEncoderKey("C", c.FormatConfig.CallerKey),
FunctionKey: GetLogEncoderKey(zapcore.OmitKey, c.FormatConfig.FunctionKey),
MessageKey: GetLogEncoderKey("M", c.FormatConfig.MessageKey),
StacktraceKey: GetLogEncoderKey("S", c.FormatConfig.StacktraceKey),
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: NewTimeEncoder(c.FormatConfig.TimeFmt),
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
if c.EnableColor {
encoderCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
switch c.Formatter {
case "console":
return zapcore.NewConsoleEncoder(encoderCfg)
case "json":
return zapcore.NewJSONEncoder(encoderCfg)
default:
return zapcore.NewConsoleEncoder(encoderCfg)
}
}
// GetLogEncoderKey gets user defined log output name, uses defKey if empty.
func GetLogEncoderKey(defKey, key string) string {
if key == "" {
return defKey
}
return key
}
func newConsoleCore(c *OutputConfig) (zapcore.Core, zap.AtomicLevel) {
lvl := zap.NewAtomicLevelAt(Levels[c.Level])
return zapcore.NewCore(
newEncoder(c),
zapcore.Lock(os.Stdout),
lvl), lvl
}
func newFileCore(c *OutputConfig) (zapcore.Core, zap.AtomicLevel, error) {
opts := []rollwriter.Option{
rollwriter.WithMaxAge(c.WriteConfig.MaxAge),
rollwriter.WithMaxBackups(c.WriteConfig.MaxBackups),
rollwriter.WithCompress(c.WriteConfig.Compress),
rollwriter.WithMaxSize(c.WriteConfig.MaxSize),
}
// roll by time.
if c.WriteConfig.RollType != RollBySize {
opts = append(opts, rollwriter.WithRotationTime(c.WriteConfig.TimeUnit.Format()))
}
writer, err := rollwriter.NewRollWriter(c.WriteConfig.Filename, opts...)
if err != nil {
return nil, zap.AtomicLevel{}, err
}
// write mode.
var ws zapcore.WriteSyncer
switch m := c.WriteConfig.WriteMode; m {
case 0, WriteFast:
// Use WriteFast as default mode.
// It has better performance, discards logs on full and avoid blocking service.
ws = rollwriter.NewAsyncRollWriter(writer, rollwriter.WithDropLog(true))
case WriteSync:
ws = zapcore.AddSync(writer)
case WriteAsync:
ws = rollwriter.NewAsyncRollWriter(writer, rollwriter.WithDropLog(false))
default:
return nil, zap.AtomicLevel{}, fmt.Errorf("validating WriteMode parameter: got %d, "+
"but expect one of WriteFast(%d), WriteAsync(%d), or WriteSync(%d)", m, WriteFast, WriteAsync, WriteSync)
}
// log level.
lvl := zap.NewAtomicLevelAt(Levels[c.Level])
return zapcore.NewCore(
newEncoder(c),
ws, lvl,
), lvl, nil
}
// NewTimeEncoder creates a time format encoder.
func NewTimeEncoder(format string) zapcore.TimeEncoder {
switch format {
case "":
return func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendByteString(defaultTimeFormat(t))
}
case "seconds":
return zapcore.EpochTimeEncoder
case "milliseconds":
return zapcore.EpochMillisTimeEncoder
case "nanoseconds":
return zapcore.EpochNanosTimeEncoder
default:
return func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format(format))
}
}
}
// defaultTimeFormat returns the default time format "2006-01-02 15:04:05.000",
// which performs better than https://pkg.go.dev/time#Time.AppendFormat.
func defaultTimeFormat(t time.Time) []byte {
t = t.Local()
year, month, day := t.Date()
hour, minute, second := t.Clock()
micros := t.Nanosecond() / 1000
buf := make([]byte, 23)
buf[0] = byte((year/1000)%10) + '0'
buf[1] = byte((year/100)%10) + '0'
buf[2] = byte((year/10)%10) + '0'
buf[3] = byte(year%10) + '0'
buf[4] = '-'
buf[5] = byte((month)/10) + '0'
buf[6] = byte((month)%10) + '0'
buf[7] = '-'
buf[8] = byte((day)/10) + '0'
buf[9] = byte((day)%10) + '0'
buf[10] = ' '
buf[11] = byte((hour)/10) + '0'
buf[12] = byte((hour)%10) + '0'
buf[13] = ':'
buf[14] = byte((minute)/10) + '0'
buf[15] = byte((minute)%10) + '0'
buf[16] = ':'
buf[17] = byte((second)/10) + '0'
buf[18] = byte((second)%10) + '0'
buf[19] = '.'
buf[20] = byte((micros/100000)%10) + '0'
buf[21] = byte((micros/10000)%10) + '0'
buf[22] = byte((micros/1000)%10) + '0'
return buf
}
// zapLog is a Logger implementation based on zaplogger.
type zapLog struct {
levels []zap.AtomicLevel
logger *zap.Logger
}
func (l *zapLog) WithOptions(opts ...Option) Logger {
o := &options{}
for _, opt := range opts {
opt(o)
}
return &zapLog{
levels: l.levels,
logger: l.logger.WithOptions(zap.AddCallerSkip(o.skip)),
}
}
// With add user defined fields to Logger. Fields support multiple values.
func (l *zapLog) With(fields ...Field) Logger {
zapFields := make([]zap.Field, len(fields))
for i := range fields {
zapFields[i] = zap.Any(fields[i].Key, fields[i].Value)
}
return &zapLog{
levels: l.levels,
logger: l.logger.With(zapFields...)}
}
func getLogMsg(args ...interface{}) string {
msg := fmt.Sprint(args...)
report.LogWriteSize.IncrBy(float64(len(msg)))
return msg
}
func getLogMsgf(format string, args ...interface{}) string {
msg := fmt.Sprintf(format, args...)
report.LogWriteSize.IncrBy(float64(len(msg)))
return msg
}
// Trace logs to TRACE log. Arguments are handled in the manner of fmt.Print.
func (l *zapLog) Trace(args ...interface{}) {
if l.logger.Core().Enabled(zapcore.DebugLevel) {
l.logger.Debug(getLogMsg(args...))
}
}
// Tracef logs to TRACE log. Arguments are handled in the manner of fmt.Printf.
func (l *zapLog) Tracef(format string, args ...interface{}) {
if l.logger.Core().Enabled(zapcore.DebugLevel) {
l.logger.Debug(getLogMsgf(format, args...))
}
}
// Debug logs to DEBUG log. Arguments are handled in the manner of fmt.Print.
func (l *zapLog) Debug(args ...interface{}) {
if l.logger.Core().Enabled(zapcore.DebugLevel) {
l.logger.Debug(getLogMsg(args...))
}
}
// Debugf logs to DEBUG log. Arguments are handled in the manner of fmt.Printf.
func (l *zapLog) Debugf(format string, args ...interface{}) {
if l.logger.Core().Enabled(zapcore.DebugLevel) {
l.logger.Debug(getLogMsgf(format, args...))
}
}
// Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
func (l *zapLog) Info(args ...interface{}) {
if l.logger.Core().Enabled(zapcore.InfoLevel) {
l.logger.Info(getLogMsg(args...))
}
}
// Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
func (l *zapLog) Infof(format string, args ...interface{}) {
if l.logger.Core().Enabled(zapcore.InfoLevel) {
l.logger.Info(getLogMsgf(format, args...))
}
}
// Warn logs to WARNING log. Arguments are handled in the manner of fmt.Print.
func (l *zapLog) Warn(args ...interface{}) {
if l.logger.Core().Enabled(zapcore.WarnLevel) {
l.logger.Warn(getLogMsg(args...))
}
}
// Warnf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
func (l *zapLog) Warnf(format string, args ...interface{}) {
if l.logger.Core().Enabled(zapcore.WarnLevel) {
l.logger.Warn(getLogMsgf(format, args...))
}
}
// Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
func (l *zapLog) Error(args ...interface{}) {
if l.logger.Core().Enabled(zapcore.ErrorLevel) {
l.logger.Error(getLogMsg(args...))
}
}
// Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
func (l *zapLog) Errorf(format string, args ...interface{}) {
if l.logger.Core().Enabled(zapcore.ErrorLevel) {
l.logger.Error(getLogMsgf(format, args...))
}
}
// Fatal logs to FATAL log. Arguments are handled in the manner of fmt.Print.
func (l *zapLog) Fatal(args ...interface{}) {
if l.logger.Core().Enabled(zapcore.FatalLevel) {
l.logger.Fatal(getLogMsg(args...))
}
}
// Fatalf logs to FATAL log. Arguments are handled in the manner of fmt.Printf.
func (l *zapLog) Fatalf(format string, args ...interface{}) {
if l.logger.Core().Enabled(zapcore.FatalLevel) {
l.logger.Fatal(getLogMsgf(format, args...))
}
}
// Sync calls the zap logger's Sync method, and flushes any buffered log entries.
// Applications should take care to call Sync before exiting.
func (l *zapLog) Sync() error {
return l.logger.Sync()
}
// SetLevel sets output log level.
func (l *zapLog) SetLevel(output string, level Level) {
i, e := strconv.Atoi(output)
if e != nil {
return
}
if i < 0 || i >= len(l.levels) {
return
}
l.levels[i].SetLevel(levelToZapLevel[level])
}
// GetLevel gets output log level.
func (l *zapLog) GetLevel(output string) Level {
i, e := strconv.Atoi(output)
if e != nil {
return LevelDebug
}
if i < 0 || i >= len(l.levels) {
return LevelDebug
}
return zapLevelToLevel[l.levels[i].Level()]
}
// CustomTimeFormat customize time format.
// Deprecated: Use https://pkg.go.dev/time#Time.Format instead.
func CustomTimeFormat(t time.Time, format string) string {
return t.Format(format)
}
// DefaultTimeFormat returns the default time format "2006-01-02 15:04:05.000".
// Deprecated: Use https://pkg.go.dev/time#Time.AppendFormat instead.
func DefaultTimeFormat(t time.Time) []byte {
return defaultTimeFormat(t)
}