-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
131 lines (103 loc) · 2.91 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
// Package banderlog is wrapper around zap logger
// that initialize stdout and JSON file logging.
package banderlog
import (
"log"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
DebugLevel = "DEBUG"
InfoLevel = "INFO"
WarnLevel = "WARN"
ErrorLevel = "ERROR"
)
type Log struct{ core *zap.SugaredLogger }
func New() *Log {
lvl := os.Getenv("LOG_LEVEL")
if lvl == "" {
lvl = DebugLevel
}
atomLvl, err := zap.ParseAtomicLevel(lvl)
if err != nil {
log.Panicf("failed to parse logger level: %v", err)
}
encConsoleCfg := zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
TimeKey: "time",
NameKey: "name",
CallerKey: "caller",
FunctionKey: "",
StacktraceKey: "stacktrace",
SkipLineEnding: false,
LineEnding: "\n",
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.NanosDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
NewReflectedEncoder: nil,
ConsoleSeparator: "\t",
}
encFileCfg := encConsoleCfg
encFileCfg.EncodeLevel = zapcore.CapitalLevelEncoder
file, err := os.Create(os.Getenv("LOG_FILE"))
if err != nil {
log.Fatalf("failed creating logger file : %v", err)
}
cores := []zapcore.Core{
zapcore.NewCore(zapcore.NewConsoleEncoder(encConsoleCfg), zapcore.Lock(os.Stderr), atomLvl.Level()),
zapcore.NewCore(zapcore.NewJSONEncoder(encFileCfg), zapcore.Lock(file), atomLvl.Level()),
}
core := zapcore.NewTee(cores...)
return &Log{zap.New(core).Sugar()}
}
func (log *Log) Level() string {
return log.core.Level().String()
}
func (log *Log) Flush() error {
return log.core.Sync()
}
func (log *Log) Debugf(format string, keyVal ...any) {
log.core.Debugf(format, keyVal...)
}
func (log *Log) Debugw(format string, keyVal ...any) {
log.core.Debugw(format, keyVal...)
}
func (log *Log) Infof(format string, keyVal ...any) {
log.core.Infof(format, keyVal...)
}
func (log *Log) Infow(format string, keyVal ...any) {
log.core.Infow(format, keyVal...)
}
func (log *Log) Warnf(format string, keyVal ...any) {
log.core.Warnf(format, keyVal...)
}
func (log *Log) Warnw(format string, keyVal ...any) {
log.core.Warnw(format, keyVal...)
}
func (log *Log) Errorf(format string, keyVal ...any) {
log.core.Errorf(format, keyVal...)
}
func (log *Log) Errorw(format string, keyVal ...any) {
log.core.Errorw(format, keyVal...)
}
func (log *Log) Fatal(keyVal ...any) {
log.core.Fatal(keyVal...)
}
func (log *Log) Fatalf(format string, keyVal ...any) {
log.core.Fatalf(format, keyVal...)
}
func (log *Log) Print(keyVal ...any) {
log.core.Info(keyVal...)
}
func (log *Log) Println(keyVal ...any) {
log.core.Infoln(keyVal...)
}
func (log *Log) Printf(format string, keyVal ...any) {
log.core.Infof(format, keyVal...)
}
func (log *Log) With(keyVal ...any) {
log.core.With(keyVal...)
}