-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
84 lines (77 loc) · 2.11 KB
/
init.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 mylog
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"time"
)
var encoderConfig = zapcore.EncoderConfig{
TimeKey: "timer",
LevelKey: "level",
NameKey: "name",
CallerKey: "caller",
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: "\n",
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: encodeTime, //zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
func encodeTime(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05.999999999"))
}
func initLog() *zap.SugaredLogger {
zapConfig := zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
Development: false,
DisableStacktrace: true,
Encoding: "console", //"json",
EncoderConfig: encoderConfig,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stdout"},
DisableCaller: false,
}
zapLogger, _ := zapConfig.Build(zap.AddCallerSkip(1))
return zapLogger.Sugar()
}
func NewLogger(name string, level int) *Logger {
return &Logger{
name: name,
level: level,
log: initLog(),
}
}
func initDefaultLog(fileOut *lumberjack.Logger) *zap.SugaredLogger {
if fileOut == nil {
fileOut = &lumberjack.Logger{
Filename: "./logs/mylog.log", // log path
MaxSize: 100, // log file size, M
MaxBackups: 30, // backups num
MaxAge: 7, // log save days
LocalTime: true,
Compress: false,
}
}
// zap log
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderConfig),
zapcore.NewMultiWriteSyncer(
zapcore.AddSync(fileOut),
zapcore.AddSync(os.Stdout),
),
zap.NewAtomicLevelAt(zapcore.DebugLevel),
)
// log
caller := zap.AddCaller()
zapLogger := zap.New(core, caller, zap.AddCallerSkip(1))
return zapLogger.Sugar()
}
func NewLoggerDefault(name string, level int, fileOut *lumberjack.Logger) *Logger {
return &Logger{
name: name,
level: level,
log: initDefaultLog(fileOut),
}
}