-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
zap.go
113 lines (94 loc) · 2.39 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
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
package logger
import (
"fmt"
"io"
"os"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var _ Logger = &zapLogger{}
type zapLogger struct {
*zap.SugaredLogger
level zap.AtomicLevel
name string
fields []interface{}
callerSkip int
}
func makeEncoderConfig(cfg Config) zapcore.EncoderConfig {
encoderConfig := zap.NewProductionEncoderConfig()
if !cfg.UnixTS {
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
}
encoderConfig.EncodeLevel = encodeLevel
return encoderConfig
}
func (l *zapLogger) SetLogLevel(lvl zapcore.Level) {
l.level.SetLevel(lvl)
}
func (l *zapLogger) With(args ...interface{}) Logger {
newLogger := *l
newLogger.SugaredLogger = l.SugaredLogger.With(args...)
newLogger.fields = copyFields(l.fields, args...)
return &newLogger
}
// copyFields returns a copy of fields with add appended.
func copyFields(fields []interface{}, add ...interface{}) []interface{} {
f := make([]interface{}, 0, len(fields)+len(add))
f = append(f, fields...)
f = append(f, add...)
return f
}
func joinName(old, new string) string {
if old == "" {
return new
}
return old + "." + new
}
func (l *zapLogger) Named(name string) Logger {
newLogger := *l
newLogger.name = joinName(l.name, name)
newLogger.SugaredLogger = l.SugaredLogger.Named(name)
newLogger.Trace("Named logger created")
return &newLogger
}
func (l *zapLogger) Helper(skip int) Logger {
newLogger := *l
newLogger.SugaredLogger = l.sugaredHelper(skip)
newLogger.callerSkip += skip
return &newLogger
}
func (l *zapLogger) sugaredHelper(skip int) *zap.SugaredLogger {
return l.SugaredLogger.Desugar().WithOptions(zap.AddCallerSkip(skip)).Sugar()
}
func (l *zapLogger) ErrorIf(err error, msg string) {
if err != nil {
l.Helper(1).Errorw(msg, "err", err)
}
}
func (l *zapLogger) ErrorIfClosing(c io.Closer, name string) {
if err := c.Close(); err != nil {
l.Helper(1).Errorw(fmt.Sprintf("Error closing %s", name), "err", err)
}
}
func (l *zapLogger) Sync() error {
err := l.SugaredLogger.Sync()
if err == nil {
return nil
}
var msg string
if uw := errors.Unwrap(err); uw != nil {
msg = uw.Error()
} else {
msg = err.Error()
}
switch msg {
case os.ErrInvalid.Error(), "bad file descriptor",
"inappropriate ioctl for device":
return nil
}
return err
}
func (l *zapLogger) Recover(panicErr interface{}) {
l.Criticalw("Recovered goroutine panic", "panic", panicErr)
}