/
log.go
51 lines (45 loc) · 1.37 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
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"reflect"
"github.com/mitchellh/copystructure"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func init() {
// Register a special copy handler for zap.AtomicLevel where the same
// instance is returned instead of creating a copy. This hack is needed
// because zap.AtomicLevel contains a private *atomic.Int32.
copystructure.Copiers[reflect.TypeOf(zap.AtomicLevel{})] = func(i interface{}) (interface{}, error) {
return i, nil
}
}
func newLogger(config *zap.Config) (*zap.Logger, error) {
logger, err := config.Build()
if err != nil {
return nil, fmt.Errorf("build zap logger: %w", err)
}
return logger, nil
}
func getDefaultLogConfig(level zapcore.Level) *zap.Config {
return &zap.Config{
Level: zap.NewAtomicLevelAt(level),
Development: level == zapcore.DebugLevel,
Encoding: "console",
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseColorLevelEncoder,
EncodeTime: zapcore.RFC3339TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
}