-
Notifications
You must be signed in to change notification settings - Fork 31
/
zap.go
86 lines (69 loc) Β· 1.92 KB
/
zap.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
package clogger
import (
"context"
"github.com/gocopper/copper/cerrors"
"github.com/gocopper/copper/clifecycle"
"go.uber.org/zap"
)
// NewZapLogger creates a Logger that internally uses go.uber.org/zap for logging
func NewZapLogger(config Config, lc *clifecycle.Lifecycle) (Logger, error) {
const OutStdErr = "stderr"
var (
outPath = OutStdErr
errOutPath = OutStdErr
)
if config.Out != "" {
outPath = config.Out
}
if config.Err != "" {
errOutPath = config.Err
}
encoderConfig := zap.NewDevelopmentEncoderConfig()
if config.Format == FormatJSON {
encoderConfig = zap.NewProductionEncoderConfig()
}
z, err := zap.Config{
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
Encoding: formatToZapEncoding(config.Format),
EncoderConfig: encoderConfig,
OutputPaths: []string{outPath},
ErrorOutputPaths: []string{errOutPath},
}.Build(zap.AddCallerSkip(1))
if err != nil {
return nil, cerrors.New(err, "failed to create zap logger", nil)
}
lc.OnStop(func(ctx context.Context) error {
// Skip sync if logs are written to stderr because it will throw an error:
// https://github.com/uber-go/zap/issues/880
if outPath == OutStdErr && errOutPath == OutStdErr {
return nil
}
return z.Sync()
})
return &zapLogger{
zap: z.Sugar(),
tags: make(map[string]interface{}),
}, nil
}
type zapLogger struct {
zap *zap.SugaredLogger
tags map[string]interface{}
}
func (l *zapLogger) WithTags(tags map[string]interface{}) Logger {
return &zapLogger{
zap: l.zap,
tags: mergeTags(l.tags, tags),
}
}
func (l *zapLogger) Debug(msg string) {
l.zap.Debugw(msg, tagsToKVs(l.tags)...)
}
func (l *zapLogger) Info(msg string) {
l.zap.Infow(msg, tagsToKVs(l.tags)...)
}
func (l *zapLogger) Warn(msg string, err error) {
l.zap.With("error", err).Warnw(msg, tagsToKVs(l.tags)...)
}
func (l *zapLogger) Error(msg string, err error) {
l.zap.With("error", err).Errorw(msg, tagsToKVs(l.tags)...)
}