-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
84 lines (64 loc) · 1.78 KB
/
logger.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
package zerowater
import (
"github.com/ThreeDotsLabs/watermill"
"github.com/rs/zerolog"
)
type ZerologLoggerAdapter struct {
logger zerolog.Logger
}
// Logs an error message.
func (loggerAdapter *ZerologLoggerAdapter) Error(msg string, err error, fields watermill.LogFields) {
event := loggerAdapter.logger.Err(err)
if fields != nil {
addWatermillFieldsData(event, fields)
}
event.Msg(msg)
}
// Logs an info message.
func (loggerAdapter *ZerologLoggerAdapter) Info(msg string, fields watermill.LogFields) {
event := loggerAdapter.logger.Info()
if fields != nil {
addWatermillFieldsData(event, fields)
}
event.Msg(msg)
}
// Logs a debug message.
func (loggerAdapter *ZerologLoggerAdapter) Debug(msg string, fields watermill.LogFields) {
event := loggerAdapter.logger.Debug()
if fields != nil {
addWatermillFieldsData(event, fields)
}
event.Msg(msg)
}
// Logs a trace.
func (loggerAdapter *ZerologLoggerAdapter) Trace(msg string, fields watermill.LogFields) {
event := loggerAdapter.logger.Trace()
if fields != nil {
addWatermillFieldsData(event, fields)
}
event.Msg(msg)
}
// Creates new adapter wiht the input fields as context.
func (loggerAdapter *ZerologLoggerAdapter) With(fields watermill.LogFields) watermill.LoggerAdapter {
if fields == nil {
return loggerAdapter
}
subLog := loggerAdapter.logger.With()
for i, v := range fields {
subLog = subLog.Interface(i, v)
}
return &ZerologLoggerAdapter{
logger: subLog.Logger(),
}
}
// Gets a new zerolog adapter for use in the watermill context.
func NewZerologLoggerAdapter(logger zerolog.Logger) *ZerologLoggerAdapter {
return &ZerologLoggerAdapter{
logger: logger,
}
}
func addWatermillFieldsData(event *zerolog.Event, fields watermill.LogFields) {
for i, v := range fields {
event.Interface(i, v)
}
}