/
log.go
40 lines (36 loc) · 1.1 KB
/
log.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
package log
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// NewLogger initializes standard zap.Logger from the provided arguments.
// By default, level field is represented as numeric.
func NewLogger(level zapcore.Level, fields ...zapcore.Field) (*zap.Logger, error) {
// Create a zap logger instance
cfg := zapConfig(zap.NewAtomicLevelAt(level))
return cfg.Build(zap.Fields(fields...))
}
func zapConfig(level zap.AtomicLevel) zap.Config {
return zap.Config{
Level: level,
Encoding: "json",
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "timestamp",
LevelKey: "level",
MessageKey: "short_message",
CallerKey: "caller",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.RFC3339NanoTimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
DisableStacktrace: true,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
}