-
Notifications
You must be signed in to change notification settings - Fork 0
/
zerolog.go
196 lines (156 loc) · 3.89 KB
/
zerolog.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
195
196
package zlog
import (
"io"
"github.com/rs/zerolog"
)
// Trivial implementation using zerolog
type logImpl struct {
zerolog.Logger
name string
lvl Level
}
func (l logImpl) Name() string {
return l.name
}
func (l logImpl) LogLevel() Level {
return l.lvl
}
func (l logImpl) Test(lvl Level) bool {
return lvl >= l.lvl
}
func (l logImpl) Write(b []byte) (int, error) {
return l.Logger.Write(b)
}
func (l logImpl) Stream(w io.Writer) Logger {
return logImpl{l.Logger.Output(w), l.name, l.lvl}
}
func (l logImpl) Level(lvl Level) Logger {
zlvl := zerolog.Level(lvl)
return logImpl{l.Logger.Level(zlvl), l.name, lvl}
}
func (l logImpl) With() Context {
zctx := l.Logger.With()
l.Logger = zctx.Logger()
return ctxImpl{zctx, l}
}
func (l logImpl) Debug() Event {
return eventImpl{l.Logger.Debug()}
}
func (l logImpl) Info() Event {
return eventImpl{l.Logger.Info()}
}
func (l logImpl) Warn() Event {
return eventImpl{l.Logger.Warn()}
}
func (l logImpl) Error() Event {
return eventImpl{l.Logger.Error()}
}
func (l logImpl) Fatal() Event {
return eventImpl{l.Logger.Fatal()}
}
func (l logImpl) Panic() Event {
return eventImpl{l.Logger.Panic()}
}
func newZeroLogger(name string, w io.Writer, lvl Level) Logger {
zl := zerolog.New(w).Level(zerolog.Level(lvl)).With().Str("name", name).Logger()
return logImpl{zl, name, Debug}
}
type eventImpl struct {
*zerolog.Event
}
func (e eventImpl) Str(key string, val string) Event {
e.Event = e.Event.Str(key, val)
return e
}
func (e eventImpl) Int(key string, val int) Event {
e.Event = e.Event.Int(key, val)
return e
}
func (e eventImpl) Enabled() bool {
return e.Event.Enabled()
}
func (e eventImpl) Msg(msg string) {
e.Event.Msg(msg)
}
func (e eventImpl) Error(er error) Event {
e.Event.Err(er)
return e
}
func (e eventImpl) Bool(key string, val bool) Event {
e.Event.Bool(key, val)
return e
}
// Timestamp adds the current local time as UNIX timestamp to the *Event context with the "time" key.
// To customize the key name, change zerolog.TimestampFieldName.
func (e eventImpl) Timestamp() Event {
e.Event.Timestamp()
return e
}
type zerologMarshalerAdapter struct {
om ObjectMarshaler
}
func (gzm zerologMarshalerAdapter) MarshalZerologObject(e *zerolog.Event) {
ne := eventImpl{e}
gzm.om.Marshal(ne)
}
func (e eventImpl) Object(key string, obj ObjectMarshaler) Event {
gzm := zerologMarshalerAdapter{obj}
e.Event.Object(key, gzm)
return e
}
// Uint adds the field key with i as a uint to the Event context.
func (e eventImpl) Uint(key string, i uint) Event {
e.Event.Uint(key, i)
return e
}
// Float32 adds the field key with f as a float32 to the *Event context.
func (e eventImpl) Float32(key string, f float32) Event {
e.Event.Float32(key, f)
return e
}
type ctxImpl struct {
zerolog.Context
l logImpl
}
func (c ctxImpl) Logger() Logger {
c.l.Logger = c.Context.Logger()
return c.l
}
func (c ctxImpl) Str(key string, val string) Context {
c.Context = c.Context.Str(key, val)
return c
}
func (c ctxImpl) Int(key string, val int) Context {
c.Context = c.Context.Int(key, val)
return c
}
// Err adds the field "error" with err as a string to the logger context.
// To customize the key name, change zerolog.ErrorFieldName.
func (c ctxImpl) Error(err error) Context {
if err != nil {
c.Context = c.Context.Err(err)
}
return c
}
// Bool adds the field key with val as a bool to the logger context.
func (c ctxImpl) Bool(key string, val bool) Context {
c.Context = c.Context.Bool(key, val)
return c
}
func (c ctxImpl) Object(key string, obj ObjectMarshaler) Context {
gzm := zerologMarshalerAdapter{obj}
c.Context = c.Context.Object(key, gzm)
return c
}
func (c ctxImpl) Uint(key string, i uint) Context {
c.Context = c.Context.Uint(key, i)
return c
}
func (c ctxImpl) Float32(key string, f float32) Context {
c.Context = c.Context.Float32(key, f)
return c
}
func (c ctxImpl) Timestamp() Context {
c.Context = c.Context.Timestamp()
return c
}