-
Notifications
You must be signed in to change notification settings - Fork 211
/
zap.go
194 lines (157 loc) · 5.08 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package log
import (
"fmt"
"reflect"
"runtime/debug"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Log is an exported type that embeds our logger.
type Log struct {
logger *zap.Logger
}
// Exported from Log basic logging options.
// Info prints formatted info level log message.
func (l Log) Info(format string, args ...interface{}) {
l.logger.Sugar().Infof(format, args...)
}
// Debug prints formatted debug level log message.
func (l Log) Debug(format string, args ...interface{}) {
l.logger.Sugar().Debugf(format, args...)
}
// Error prints formatted error level log message.
func (l Log) Error(format string, args ...interface{}) {
l.logger.Sugar().Errorf(format, args...)
}
// Warning prints formatted warning level log message.
func (l Log) Warning(format string, args ...interface{}) {
l.logger.Sugar().Warnf(format, args...)
}
// Panic prints the log message and then panics.
func (l Log) Panic(format string, args ...interface{}) {
l.logger.Sugar().Error("Fatal: goroutine panicked. Stacktrace: ", string(debug.Stack()))
l.logger.Sugar().Panicf(format, args...)
}
// Wrap and export field logic
// Field is a log field holding a name and value
type Field zap.Field
// Field satisfy loggable field interface.
func (f Field) Field() Field { return f }
// FieldNamed returns a field with the provided name instead of the default.
func FieldNamed(name string, field LoggableField) Field {
if field == nil || (reflect.ValueOf(field).Kind() == reflect.Ptr && reflect.ValueOf(field).IsNil()) {
return String(name, "nil")
}
f := field.Field()
f.Key = name
return f
}
// String returns a string Field
func String(name, val string) Field {
return Field(zap.String(name, val))
}
// Int returns an int Field.
func Int(name string, val int) Field {
return Field(zap.Int(name, val))
}
// Int32 returns an int32 Field.
func Int32(name string, val int32) Field {
return Field(zap.Int32(name, val))
}
// Uint32 returns an uint32 Field.
func Uint32(name string, val uint32) Field {
return Field(zap.Uint32(name, val))
}
// Uint64 returns an uint64 Field
func Uint64(name string, val uint64) Field {
return Field(zap.Uint64(name, val))
}
// Namespace make next fields be inside a namespace.
func Namespace(name string) Field {
return Field(zap.Namespace(name))
}
// Bool returns a bool field
func Bool(name string, val bool) Field {
return Field(zap.Bool(name, val))
}
// Duration returns a duration field
func Duration(name string, val time.Duration) Field {
return Field(zap.Duration(name, val))
}
// Err returns an error field
func Err(v error) Field {
return Field(zap.NamedError("errmsg", v))
}
// LoggableField as an interface to enable every type to be used as a log field.
type LoggableField interface {
Field() Field
}
func unpack(fields []LoggableField) []zap.Field {
flds := make([]zap.Field, len(fields))
for i, f := range fields {
flds[i] = zap.Field(f.Field())
}
return flds
}
// FieldLogger is a logger that only logs messages with fields. It does not support formatting.
type FieldLogger struct {
l *zap.Logger
}
// With returns a logger object that logs fields
func (l Log) With() FieldLogger {
return FieldLogger{l.logger}
}
// SetLevel returns a logger with level as the log level derived from l.
func (l Log) SetLevel(level *zap.AtomicLevel) Log {
// Warn if the new level is lower than the parent level
if willWrite := l.logger.Check(level.Level(), "test"); willWrite == nil {
Warning("attempt to SetLevel of logger lower than parent level, this may result in " +
"log entries being dropped silently")
}
lgr := l.logger.WithOptions(zap.IncreaseLevel(level))
return Log{
lgr,
}
}
// WithName returns a logger with the given name
func (l Log) WithName(prefix string) Log {
lgr := l.logger.Named(fmt.Sprintf("%-13s", prefix))
return Log{lgr}
}
// WithFields returns a logger with fields permanently appended to it.
func (l Log) WithFields(fields ...LoggableField) Log {
lgr := l.logger.With(unpack(fields)...)
return Log{logger: lgr}
}
const eventKey = "event"
// Event returns a logger with the Event field appended to it.
func (l Log) Event() FieldLogger {
return FieldLogger{l: l.logger.With(zap.Field(Bool(eventKey, true)))}
}
// Nop is an option that disables this logger.
var Nop = zap.WrapCore(func(zapcore.Core) zapcore.Core {
return zapcore.NewNopCore()
})
// WithOptions clones the current Logger, applies the supplied Options, and
// returns the resulting Logger. It's safe to use concurrently.
func (l Log) WithOptions(opts ...zap.Option) Log {
lgr := l.logger.WithOptions(opts...)
return Log{logger: lgr}
}
// Info prints message with fields
func (fl FieldLogger) Info(msg string, fields ...LoggableField) {
fl.l.Info(msg, unpack(fields)...)
}
// Debug prints message with fields
func (fl FieldLogger) Debug(msg string, fields ...LoggableField) {
fl.l.Debug(msg, unpack(fields)...)
}
// Error prints message with fields
func (fl FieldLogger) Error(msg string, fields ...LoggableField) {
fl.l.Error(msg, unpack(fields)...)
}
// Warning prints message with fields
func (fl FieldLogger) Warning(msg string, fields ...LoggableField) {
fl.l.Warn(msg, unpack(fields)...)
}