-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogging.go
56 lines (48 loc) · 1.12 KB
/
logging.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
package loaderbot
import (
"fmt"
jsoniter "github.com/json-iterator/go"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Logger struct {
*zap.SugaredLogger
}
func (m *Logger) With(args ...interface{}) *Logger {
m.SugaredLogger = m.SugaredLogger.With(args...)
return m
}
func (m *Logger) Clone() Logger {
return *m
}
func setupLogger(encoding string, level string) *Logger {
rawJSON := []byte(fmt.Sprintf(`{
"level": "%s",
"encoding": "%s",
"outputPaths": ["stdout"],
"errorOutputPaths": ["stderr"],
"encoderConfig": {
"messageKey": "message",
"levelKey": "level",
"levelEncoder": "uppercase",
"timeKey": "time",
"timeEncoder": "ISO8601",
"callerKey": "caller",
"callerEncoder": "short"
}
}`, level, encoding))
var cfg zap.Config
if err := jsoniter.Unmarshal(rawJSON, &cfg); err != nil {
panic(err)
}
cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
logger, err := cfg.Build()
if err != nil {
panic(err)
}
_ = logger.Sync()
return &Logger{logger.Sugar()}
}
func NewLogger(cfg *RunnerConfig) *Logger {
return setupLogger(cfg.LogEncoding, cfg.LogLevel)
}