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
|
package log
import (
"os"
"sync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
var (
logger *zap.Logger
once sync.Once
)
var (
Debug func(msg string, fields ...zap.Field)
Info func(msg string, fields ...zap.Field)
Warn func(msg string, fields ...zap.Field)
Error func(msg string, fields ...zap.Field)
Fatal func(msg string, fields ...zap.Field)
String = zap.String
Int = zap.Int
Err = zap.Error
Any = zap.Any
Duration = zap.Duration
)
func InitLog() {
once.Do(func() {
logger = logConfig()
})
Debug = logger.Debug
Info = logger.Info
Warn = logger.Warn
Error = logger.Error
Fatal = logger.Fatal
}
func logConfig() *zap.Logger {
homePath := os.Getenv("HOME")
// 日志文件分割配置
fileWriterHook := &lumberjack.Logger{
Filename: homePath + "/var/log/tiktokserver/server.log",
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
Compress: true, // disabled by default
}
// 日志文件输出配置
fileEncoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "line",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder, // 全大写日志等级标识
EncodeTime: zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05"), // 时间格式
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeName: zapcore.FullNameEncoder,
}
// 终端输出配置
stdEncoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "line",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05"),
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeName: zapcore.FullNameEncoder,
}
fileEncoder := zapcore.NewJSONEncoder(fileEncoderConfig)
stdEncoder := zapcore.NewConsoleEncoder(stdEncoderConfig)
fileWriter := zapcore.NewMultiWriteSyncer(zapcore.AddSync(fileWriterHook))
stdWriter := zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout))
// 日志级别过滤
debugLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.DebugLevel
})
// infoLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
// return lvl >= zapcore.InfoLevel
// })
warnLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.WarnLevel
})
core := zapcore.NewTee(
zapcore.NewCore(fileEncoder, fileWriter, warnLevel),
zapcore.NewCore(stdEncoder, stdWriter, debugLevel),
)
if os.Getenv("GIN_DEBUG") == "true" {
caller := zap.AddCaller()
development := zap.Development()
return zap.New(core, caller, development)
} else {
return zap.New(core)
}
}
func Sync() {
logger.Sync()
}
|