-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
177 lines (160 loc) · 4.24 KB
/
logger.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
package giu
import (
"context"
"errors"
"io"
"log/slog"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
type LoggerParams struct {
LogName string // log file name with path
LogLevel string // log level: info, debug, warn, error, dpanic, panic, fatal
MaxSize int // size in megabytes
MaxBackup int // max backup files
MaxAge int // max age in days
Compress bool // compress
Tag string // log tag
}
var (
ERR_LOGGER_NOT_INIT = errors.New("logger is nil, please init logger first")
)
const (
LOG_LEVEL_DEBUG = "debug"
LOG_LEVEL_INFO = "info"
LOG_LEVEL_WARN = "warn"
LOG_LEVEL_ERROR = "error"
LOG_LEVEL_PANIC = "panic"
LOG_LEVEL_DPANIC = "dpanic"
LOG_LEVEL_FATAL = "fatal"
)
var _defaultLoggerParams = LoggerParams{
LogName: "logs/log.log",
LogLevel: "debug",
MaxSize: 10,
MaxBackup: 10,
MaxAge: 7,
Compress: true,
Tag: "default",
}
func NewZapLogger(params *LoggerParams) *zap.Logger {
core := newZapCore(params.LogName, params.LogLevel, params.MaxSize, params.MaxBackup, params.MaxAge, params.Compress)
return zap.New(core, zap.AddCaller(), zap.Development(), zap.Fields(zap.String("tag", params.Tag)))
}
func DefaultZapLogger() *zap.Logger {
return NewZapLogger(&_defaultLoggerParams)
}
func newZapCore(fileName string, level string, maxSize int, maxBackups int, maxAge int, compress bool) zapcore.Core {
hook := lumberjack.Logger{
Filename: fileName,
MaxSize: maxSize,
MaxBackups: maxBackups,
MaxAge: maxAge,
Compress: compress,
}
atomicLevel := zap.NewAtomicLevel()
logLevel := convertZapLevel(level)
atomicLevel.SetLevel(logLevel)
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.MillisDurationEncoder,
// EncodeCaller: zapcore.FullCallerEncoder,
EncodeName: zapcore.FullNameEncoder,
}
syncer := zapcore.AddSync(&hook)
if logLevel <= zapcore.InfoLevel {
// log to stdout when log level is info or lower
syncer = zapcore.NewMultiWriteSyncer(syncer, zapcore.AddSync(os.Stdout))
}
return zapcore.NewCore(
zapcore.NewJSONEncoder(encoderConfig),
syncer,
atomicLevel,
)
}
type ZapLogger struct {
*zap.Logger
}
func (zl *ZapLogger) Printf(ctx context.Context, format string, v ...interface{}) {
if zl.Logger != nil {
zl.Logger.Sugar().Infof(format, v...)
}
}
func convertZapLevel(logLevel string) zapcore.Level {
var level zapcore.Level
switch logLevel {
case LOG_LEVEL_INFO:
level = zapcore.InfoLevel
case LOG_LEVEL_DEBUG:
level = zapcore.DebugLevel
case LOG_LEVEL_WARN:
level = zapcore.WarnLevel
case LOG_LEVEL_ERROR:
level = zapcore.ErrorLevel
case LOG_LEVEL_DPANIC:
level = zapcore.DPanicLevel
case LOG_LEVEL_PANIC:
level = zapcore.PanicLevel
case LOG_LEVEL_FATAL:
level = zapcore.FatalLevel
default:
level = zapcore.InfoLevel
}
return level
}
func convertSLogLevel(logLevel string) slog.Level {
var level slog.Level
switch logLevel {
case LOG_LEVEL_INFO:
level = slog.LevelInfo
case LOG_LEVEL_DEBUG:
level = slog.LevelDebug
case LOG_LEVEL_WARN:
level = slog.LevelWarn
case LOG_LEVEL_ERROR:
level = slog.LevelError
case LOG_LEVEL_DPANIC:
level = slog.LevelError
case LOG_LEVEL_PANIC:
level = slog.LevelError
case LOG_LEVEL_FATAL:
level = slog.LevelError
default:
level = slog.LevelInfo
}
return level
}
func NewSLogger(params LoggerParams) *slog.Logger {
var writer io.Writer
hook := lumberjack.Logger{
Filename: params.LogName,
MaxSize: params.MaxSize,
MaxBackups: params.MaxBackup,
MaxAge: params.MaxAge,
Compress: params.Compress,
}
logLevel := convertSLogLevel(params.LogLevel)
if logLevel < slog.LevelInfo {
writer = io.MultiWriter(&hook, os.Stdout)
} else {
writer = &hook
}
handler := slog.NewJSONHandler(writer, &slog.HandlerOptions{Level: logLevel})
logger := slog.New(handler)
if params.Tag != "" {
logger = logger.With(slog.String("tag", params.Tag))
}
return logger
}
func DefaultSLogger() *slog.Logger {
return NewSLogger(_defaultLoggerParams)
}