/
zap_logger.go
244 lines (211 loc) · 6.45 KB
/
zap_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2018 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The klaytn library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
package log
import (
"errors"
"fmt"
"sync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var zlManager = zapLoggerManager{
"stderr", // Use stderr to outputPath instead of stdout to be aligned with log15.
"json", zapcore.InfoLevel,
sync.Mutex{},
make(map[ModuleID][]*zapLogger),
}
type zapLoggerManager struct {
outputPath string
encodingType string
logLevel zapcore.Level
mutex sync.Mutex
loggersMap map[ModuleID][]*zapLogger
}
type zapLogger struct {
mi ModuleID
cfg *zap.Config
sl *zap.SugaredLogger
}
// A zapLogger generated from NewWith inherits InitialFields and ModuleID from its parent.
func (zl *zapLogger) NewWith(keysAndValues ...interface{}) Logger {
zlManager.mutex.Lock()
defer zlManager.mutex.Unlock()
newCfg := genDefaultConfig()
for k, v := range zl.cfg.InitialFields {
newCfg.InitialFields[k] = v
}
newCfg.Level.SetLevel(zl.cfg.Level.Level())
return genLoggerZap(zl.mi, newCfg)
}
func (zl *zapLogger) newModuleLogger(mi ModuleID) Logger {
zlManager.mutex.Lock()
defer zlManager.mutex.Unlock()
zapCfg := genDefaultConfig()
zapCfg.InitialFields[module] = mi
return genLoggerZap(mi, zapCfg)
}
func (zl *zapLogger) Trace(msg string, keysAndValues ...interface{}) {
zl.sl.Debugw(msg, keysAndValues...)
}
func (zl *zapLogger) Debug(msg string, keysAndValues ...interface{}) {
zl.sl.Debugw(msg, keysAndValues...)
}
func (zl *zapLogger) Info(msg string, keysAndValues ...interface{}) {
zl.sl.Infow(msg, keysAndValues...)
}
func (zl *zapLogger) Warn(msg string, keysAndValues ...interface{}) {
zl.sl.Warnw(msg, keysAndValues...)
}
func (zl *zapLogger) Error(msg string, keysAndValues ...interface{}) {
zl.sl.Errorw(msg, keysAndValues...)
}
func (zl *zapLogger) ErrorWithStack(msg string, keysAndValues ...interface{}) {
zl.sl.Errorw(msg, keysAndValues...)
}
func (zl *zapLogger) Crit(msg string, keysAndValues ...interface{}) {
zl.sl.Fatalw(msg, keysAndValues...)
}
func (zl *zapLogger) CritWithStack(msg string, keysAndValues ...interface{}) {
zl.sl.Fatalw(msg, keysAndValues...)
}
// GetHandler and SetHandler do nothing but exist to make consistency in Logger interface.
func (zl *zapLogger) GetHandler() Handler {
return nil
}
func (zl *zapLogger) SetHandler(h Handler) {
}
func (zl *zapLogger) setLevel(lvl Lvl) {
zl.cfg.Level.SetLevel(lvlToZapLevel(lvl))
}
// register registers the receiver to zapLoggerManager.
func (zl *zapLogger) register() {
zlManager.loggersMap[zl.mi] = append(zlManager.loggersMap[zl.mi], zl)
}
func genBaseLoggerZap() Logger {
return genLoggerZap(BaseLogger, genDefaultConfig())
}
// genLoggerZap creates a zapLogger with given ModuleID and Config.
func genLoggerZap(mi ModuleID, cfg *zap.Config) Logger {
logger, err := cfg.Build()
if err != nil {
Fatalf("Error while building zapLogger from the config. ModuleID: %v, err: %v", mi, err)
}
newLogger := &zapLogger{mi, cfg, logger.Sugar()}
newLogger.register()
return newLogger
}
// ChangeLogLevelWithName changes the log level of loggers with given ModuleName.
func ChangeLogLevelWithName(moduleName string, lvl Lvl) error {
if err := levelCheck(lvl); err != nil {
return err
}
mi := GetModuleID(moduleName)
if mi == ModuleNameLen {
return errors.New("entered module name does not match with any existing log module")
}
return ChangeLogLevelWithID(mi, lvl)
}
// ChangeLogLevelWithName changes the log level of loggers with given ModuleID.
func ChangeLogLevelWithID(mi ModuleID, lvl Lvl) error {
if err := levelCheck(lvl); err != nil {
return err
}
if err := idCheck(mi); err != nil {
return err
}
loggers := zlManager.loggersMap[mi]
for _, logger := range loggers {
logger.setLevel(lvl)
}
return nil
}
func ChangeGlobalLogLevel(glogger *GlogHandler, lvl Lvl) error {
if err := levelCheck(lvl); err != nil {
return err
}
for _, loggers := range zlManager.loggersMap {
for _, logger := range loggers {
logger.setLevel(lvl)
}
}
if glogger != nil {
glogger.Verbosity(lvl)
}
return nil
}
func levelCheck(lvl Lvl) error {
if lvl >= LvlEnd {
return errors.New(fmt.Sprintf("insert log level less than %d", LvlEnd))
}
if lvl < LvlCrit {
return errors.New(fmt.Sprintf("insert log level greater than or equal to %d", LvlCrit))
}
return nil
}
func idCheck(mi ModuleID) error {
if mi >= ModuleNameLen {
return errors.New(fmt.Sprintf("insert log level less than %d", ModuleNameLen))
}
if mi <= BaseLogger {
return errors.New(fmt.Sprintf("insert log level greater than %d", BaseLogger))
}
return nil
}
func lvlToZapLevel(lvl Lvl) zapcore.Level {
switch lvl {
case LvlCrit:
return zapcore.FatalLevel
case LvlError:
return zapcore.ErrorLevel
case LvlWarn:
return zapcore.WarnLevel
case LvlInfo:
return zapcore.InfoLevel
case LvlDebug:
return zapcore.DebugLevel
case LvlTrace:
return zapcore.DebugLevel
default:
baseLogger.Error("Unexpected log level entered. Use InfoLevel instead.", "entered level", lvl)
return zapcore.InfoLevel
}
}
func genDefaultEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
// CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
// EncodeCaller: zapcore.ShortCallerEncoder,
}
}
func genDefaultConfig() *zap.Config {
encoderConfig := genDefaultEncoderConfig()
return &zap.Config{
Encoding: zlManager.encodingType,
Level: zap.NewAtomicLevelAt(zlManager.logLevel),
OutputPaths: []string{zlManager.outputPath},
Development: false,
EncoderConfig: encoderConfig,
InitialFields: make(map[string]interface{}),
}
}