-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
113 lines (100 loc) · 2.75 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
package logger
import (
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"log"
"os"
)
const layout = "2006-01-02 15:04:05"
var (
infoCore zapcore.Core
debugCore zapcore.Core
errorCore zapcore.Core
fatalCore zapcore.Core
atom zap.AtomicLevel
)
func InitLogger() {
atom = zap.NewAtomicLevel()
switch viper.GetString("logger.level") {
case "debug":
atom.SetLevel(zap.DebugLevel)
break
case "info":
atom.SetLevel(zap.InfoLevel)
break
case "error":
atom.SetLevel(zap.ErrorLevel)
break
case "fatal":
atom.SetLevel(zap.FatalLevel)
break
default:
log.Fatalf("unknown log level: %s", viper.GetString("logger.level"))
}
infoCore = zapcore.NewCore(
zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
CallerKey: "",
EncodeTime: zapcore.TimeEncoderOfLayout(layout),
LineEnding: zapcore.DefaultLineEnding,
MessageKey: "message",
TimeKey: "timestamp",
}),
zapcore.AddSync(os.Stdout), zap.InfoLevel,
)
debugCore = zapcore.NewCore(
zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
CallerKey: "caller",
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeTime: zapcore.TimeEncoderOfLayout(layout),
LineEnding: zapcore.DefaultLineEnding,
MessageKey: "message",
TimeKey: "timestamp",
}),
zapcore.AddSync(os.Stdout), zap.DebugLevel,
)
errorCore = zapcore.NewCore(
zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
EncodeTime: zapcore.TimeEncoderOfLayout(layout),
LineEnding: zapcore.DefaultLineEnding,
MessageKey: "message",
StacktraceKey: "stacktrace",
TimeKey: "timestamp",
}),
zapcore.AddSync(os.Stdout), zap.ErrorLevel,
)
fatalCore = zapcore.NewCore(
zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
CallerKey: "caller",
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeTime: zapcore.TimeEncoderOfLayout(layout),
LineEnding: zapcore.DefaultLineEnding,
MessageKey: "message",
TimeKey: "timestamp",
}),
zapcore.AddSync(os.Stdout), zap.FatalLevel,
)
}
func GetLevel() string {
return atom.String()
}
func Infof(template string, args ...any) {
if atom.Enabled(zap.InfoLevel) {
zap.New(infoCore, zap.AddCaller()).Sugar().Infof(template, args...)
}
}
func Errorf(template string, args ...any) {
if atom.Enabled(zap.ErrorLevel) {
zap.New(errorCore, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel)).Sugar().Errorf(template, args...)
}
}
func Debugf(template string, args ...any) {
if atom.Enabled(zap.DebugLevel) {
zap.New(debugCore, zap.AddCaller(), zap.AddStacktrace(zapcore.DebugLevel)).Sugar().Debugf(template, args...)
}
}
func Fatalf(template string, args ...any) {
if atom.Enabled(zap.FatalLevel) {
zap.New(fatalCore, zap.AddCaller(), zap.AddCallerSkip(1)).Sugar().Fatalf(template, args...)
}
}