-
Notifications
You must be signed in to change notification settings - Fork 248
/
zap_adapter.go
152 lines (136 loc) · 4.02 KB
/
zap_adapter.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package logutils
import (
"fmt"
"math"
"sync"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/protocol/zaputil"
)
type gethLoggerCore struct {
zapcore.LevelEnabler
fields []zapcore.Field
logger log.Logger
}
func (c gethLoggerCore) With(fields []zapcore.Field) zapcore.Core {
clone := c.clone()
clone.fields = append(clone.fields, fields...)
return clone
}
func (c gethLoggerCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if c.Enabled(ent.Level) {
return ce.AddCore(ent, c)
}
return ce
}
func (c gethLoggerCore) Write(ent zapcore.Entry, fields []zapcore.Field) error {
fields = append(c.fields[:], fields...)
var args []interface{}
for _, f := range fields {
switch f.Type {
case zapcore.ArrayMarshalerType,
zapcore.ObjectMarshalerType,
zapcore.BinaryType,
zapcore.ByteStringType,
zapcore.Complex128Type,
zapcore.ReflectType,
zapcore.StringerType,
zapcore.ErrorType:
args = append(args, f.Key, f.Interface)
case zapcore.BoolType:
args = append(args, f.Key, f.Integer == 1)
case zapcore.DurationType:
args = append(args, f.Key, time.Duration(f.Integer))
case zapcore.Float64Type:
args = append(args, f.Key, math.Float64frombits(uint64(f.Integer)))
case zapcore.Float32Type:
args = append(args, f.Key, math.Float32frombits(uint32(f.Integer)))
case zapcore.Int64Type,
zapcore.Int32Type,
zapcore.Int16Type,
zapcore.Int8Type,
zapcore.Uint64Type,
zapcore.Uint32Type,
zapcore.Uint16Type,
zapcore.Uint8Type:
args = append(args, f.Key, f.Integer)
case zapcore.UintptrType:
args = append(args, f.Key, uintptr(f.Integer))
case zapcore.StringType:
args = append(args, f.Key, f.String)
case zapcore.TimeType:
if f.Interface != nil {
args = append(args, f.Key, time.Unix(0, f.Integer).In(f.Interface.(*time.Location)))
} else {
// Fall back to UTC if location is nil.
args = append(args, f.Key, time.Unix(0, f.Integer))
}
case zapcore.NamespaceType:
args = append(args, "namespace", f.Key)
case zapcore.SkipType:
break
default:
panic(fmt.Sprintf("unknown field type: %v", f))
}
}
// set callDepth to 3 for `Output` to skip the calls to zap.Logger
// and get the correct caller in the log
callDepth := 3
switch ent.Level {
case zapcore.DebugLevel:
c.logger.Output(ent.Message, log.LvlDebug, callDepth, args...)
case zapcore.InfoLevel:
c.logger.Output(ent.Message, log.LvlInfo, callDepth, args...)
case zapcore.WarnLevel:
c.logger.Output(ent.Message, log.LvlWarn, callDepth, args...)
case zapcore.ErrorLevel:
c.logger.Output(ent.Message, log.LvlError, callDepth, args...)
case zapcore.DPanicLevel, zapcore.PanicLevel, zapcore.FatalLevel:
c.logger.Output(ent.Message, log.LvlCrit, callDepth, args...)
}
return nil
}
func (gethLoggerCore) Sync() error {
return nil
}
func (c *gethLoggerCore) clone() *gethLoggerCore {
return &gethLoggerCore{
LevelEnabler: c.LevelEnabler,
fields: c.fields,
logger: c.logger,
}
}
// NewZapAdapter returns a new zapcore.Core interface which forwards logs to log.Logger.
func NewZapAdapter(logger log.Logger, enab zapcore.LevelEnabler) zapcore.Core {
return &gethLoggerCore{
LevelEnabler: enab,
logger: logger,
}
}
var registerOnce sync.Once
// NewZapLoggerWithAdapter returns a logger forwarding all logs with level info and above.
func NewZapLoggerWithAdapter(logger log.Logger) (*zap.Logger, error) {
registerOnce.Do(func() {
if err := zaputil.RegisterJSONHexEncoder(); err != nil {
panic(err)
}
})
cfg := zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
Development: false,
Sampling: nil,
Encoding: "json-hex",
EncoderConfig: zap.NewProductionEncoderConfig(),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
adapter := zap.WrapCore(
func(zapcore.Core) zapcore.Core {
return NewZapAdapter(logger, cfg.Level)
},
)
log.PrintOrigins(true)
return cfg.Build(adapter)
}