-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadapter.go
67 lines (59 loc) · 1.92 KB
/
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
//Package watermillzap provides the implementation of watermill.LoggerAdapter using go.uber.org/zap.
package watermillzap
import (
"github.com/ThreeDotsLabs/watermill"
"go.uber.org/zap"
)
// Logger implements watermill.LoggerAdapter with *zap.Logger.
type Logger struct {
backend *zap.Logger
fields watermill.LogFields
}
// NewLogger returns new watermill.LoggerAdapter using passed *zap.Logger as backend.
func NewLogger(z *zap.Logger) watermill.LoggerAdapter {
return &Logger{backend: z}
}
// Error writes error log with message, error and some fields.
func (l *Logger) Error(msg string, err error, fields watermill.LogFields) {
fields = l.fields.Add(fields)
fs := make([]zap.Field, 0, len(fields)+1)
fs = append(fs, zap.Error(err))
for k, v := range fields {
fs = append(fs, zap.Any(k, v))
}
l.backend.Error(msg, fs...)
}
// Info writes info log with message and some fields.
func (l *Logger) Info(msg string, fields watermill.LogFields) {
fields = l.fields.Add(fields)
fs := make([]zap.Field, 0, len(fields)+1)
for k, v := range fields {
fs = append(fs, zap.Any(k, v))
}
l.backend.Info(msg, fs...)
}
// Debug writes debug log with message and some fields.
func (l *Logger) Debug(msg string, fields watermill.LogFields) {
fields = l.fields.Add(fields)
fs := make([]zap.Field, 0, len(fields)+1)
for k, v := range fields {
fs = append(fs, zap.Any(k, v))
}
l.backend.Debug(msg, fs...)
}
// Trace writes debug log instead of trace log because zap does not support trace level logging.
func (l *Logger) Trace(msg string, fields watermill.LogFields) {
fields = l.fields.Add(fields)
fs := make([]zap.Field, 0, len(fields)+1)
for k, v := range fields {
fs = append(fs, zap.Any(k, v))
}
l.backend.Debug(msg, fs...)
}
// With returns new LoggerAdapter with passed fields.
func (l *Logger) With(fields watermill.LogFields) watermill.LoggerAdapter {
return &Logger{
backend: l.backend,
fields: l.fields.Add(fields),
}
}