forked from ThreeDotsLabs/watermill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
257 lines (208 loc) · 5.85 KB
/
log.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package watermill
import (
"fmt"
"io"
"log"
"os"
"reflect"
"sort"
"strings"
"sync"
)
// LogFields is the logger's key-value list of fields.
type LogFields map[string]interface{}
// Add adds new fields to the list of LogFields.
func (l LogFields) Add(newFields LogFields) LogFields {
resultFields := make(LogFields, len(l)+len(newFields))
for field, value := range l {
resultFields[field] = value
}
for field, value := range newFields {
resultFields[field] = value
}
return resultFields
}
// Copy copies the LogFields.
func (l LogFields) Copy() LogFields {
cpy := make(LogFields, len(l))
for k, v := range l {
cpy[k] = v
}
return cpy
}
// LoggerAdapter is an interface, that you need to implement to support Watermill logging.
// You can use StdLoggerAdapter as a reference implementation.
type LoggerAdapter interface {
Error(msg string, err error, fields LogFields)
Info(msg string, fields LogFields)
Debug(msg string, fields LogFields)
Trace(msg string, fields LogFields)
With(fields LogFields) LoggerAdapter
}
// NopLogger is a logger which discards all logs.
type NopLogger struct{}
func (NopLogger) Error(msg string, err error, fields LogFields) {}
func (NopLogger) Info(msg string, fields LogFields) {}
func (NopLogger) Debug(msg string, fields LogFields) {}
func (NopLogger) Trace(msg string, fields LogFields) {}
func (l NopLogger) With(fields LogFields) LoggerAdapter { return l }
// StdLoggerAdapter is a logger implementation, which sends all logs to provided standard output.
type StdLoggerAdapter struct {
ErrorLogger *log.Logger
InfoLogger *log.Logger
DebugLogger *log.Logger
TraceLogger *log.Logger
fields LogFields
}
// NewStdLogger creates StdLoggerAdapter which sends all logs to stderr.
func NewStdLogger(debug, trace bool) LoggerAdapter {
return NewStdLoggerWithOut(os.Stderr, debug, trace)
}
// NewStdLoggerWithOut creates StdLoggerAdapter which sends all logs to provided io.Writer.
func NewStdLoggerWithOut(out io.Writer, debug bool, trace bool) LoggerAdapter {
l := log.New(out, "[watermill] ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
a := &StdLoggerAdapter{InfoLogger: l, ErrorLogger: l}
if debug {
a.DebugLogger = l
}
if trace {
a.TraceLogger = l
}
return a
}
func (l *StdLoggerAdapter) Error(msg string, err error, fields LogFields) {
l.log(l.ErrorLogger, "ERROR", msg, fields.Add(LogFields{"err": err}))
}
func (l *StdLoggerAdapter) Info(msg string, fields LogFields) {
l.log(l.InfoLogger, "INFO ", msg, fields)
}
func (l *StdLoggerAdapter) Debug(msg string, fields LogFields) {
l.log(l.DebugLogger, "DEBUG", msg, fields)
}
func (l *StdLoggerAdapter) Trace(msg string, fields LogFields) {
l.log(l.TraceLogger, "TRACE", msg, fields)
}
func (l *StdLoggerAdapter) With(fields LogFields) LoggerAdapter {
return &StdLoggerAdapter{
ErrorLogger: l.ErrorLogger,
InfoLogger: l.InfoLogger,
DebugLogger: l.DebugLogger,
TraceLogger: l.TraceLogger,
fields: l.fields.Add(fields),
}
}
func (l *StdLoggerAdapter) log(logger *log.Logger, level string, msg string, fields LogFields) {
if logger == nil {
return
}
fieldsStr := ""
allFields := l.fields.Add(fields)
keys := make([]string, len(allFields))
i := 0
for field := range allFields {
keys[i] = field
i++
}
sort.Strings(keys)
for _, key := range keys {
var valueStr string
value := allFields[key]
if stringer, ok := value.(fmt.Stringer); ok {
valueStr = stringer.String()
} else {
valueStr = fmt.Sprintf("%v", value)
}
if strings.Contains(valueStr, " ") {
valueStr = `"` + valueStr + `"`
}
fieldsStr += key + "=" + valueStr + " "
}
_ = logger.Output(3, fmt.Sprintf("\t"+`level=%s msg="%s" %s`, level, msg, fieldsStr))
}
type LogLevel uint
const (
TraceLogLevel LogLevel = iota + 1
DebugLogLevel
InfoLogLevel
ErrorLogLevel
)
type CapturedMessage struct {
Level LogLevel
Fields LogFields
Msg string
Err error
}
// CaptureLoggerAdapter is a logger which captures all logs.
// This logger is mostly useful for testing logging.
type CaptureLoggerAdapter struct {
captured map[LogLevel][]CapturedMessage
fields LogFields
lock sync.Mutex
}
func NewCaptureLogger() *CaptureLoggerAdapter {
return &CaptureLoggerAdapter{
captured: map[LogLevel][]CapturedMessage{},
}
}
func (c *CaptureLoggerAdapter) With(fields LogFields) LoggerAdapter {
return &CaptureLoggerAdapter{captured: c.captured, fields: c.fields.Add(fields)}
}
func (c *CaptureLoggerAdapter) capture(msg CapturedMessage) {
c.lock.Lock()
defer c.lock.Unlock()
c.captured[msg.Level] = append(c.captured[msg.Level], msg)
}
func (c *CaptureLoggerAdapter) Captured() map[LogLevel][]CapturedMessage {
c.lock.Lock()
defer c.lock.Unlock()
return c.captured
}
func (c *CaptureLoggerAdapter) Has(msg CapturedMessage) bool {
c.lock.Lock()
defer c.lock.Unlock()
for _, capturedMsg := range c.captured[msg.Level] {
if reflect.DeepEqual(msg, capturedMsg) {
return true
}
}
return false
}
func (c *CaptureLoggerAdapter) HasError(err error) bool {
c.lock.Lock()
defer c.lock.Unlock()
for _, capturedMsg := range c.captured[ErrorLogLevel] {
if capturedMsg.Err == err {
return true
}
}
return false
}
func (c *CaptureLoggerAdapter) Error(msg string, err error, fields LogFields) {
c.capture(CapturedMessage{
Level: ErrorLogLevel,
Fields: c.fields.Add(fields),
Msg: msg,
Err: err,
})
}
func (c *CaptureLoggerAdapter) Info(msg string, fields LogFields) {
c.capture(CapturedMessage{
Level: InfoLogLevel,
Fields: c.fields.Add(fields),
Msg: msg,
})
}
func (c *CaptureLoggerAdapter) Debug(msg string, fields LogFields) {
c.capture(CapturedMessage{
Level: DebugLogLevel,
Fields: c.fields.Add(fields),
Msg: msg,
})
}
func (c *CaptureLoggerAdapter) Trace(msg string, fields LogFields) {
c.capture(CapturedMessage{
Level: TraceLogLevel,
Fields: c.fields.Add(fields),
Msg: msg,
})
}