-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogrus_adapter_test.go
76 lines (56 loc) · 1.96 KB
/
logrus_adapter_test.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
/*
* watermill-logrus-adapter
* Copyright (c) 2022, Matthias Hartmann <mahartma@mahartma.com>
*
* For license see LICENSE
*/
package wla
import (
"bytes"
"testing"
"github.com/ThreeDotsLabs/watermill"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func setup() (*logrus.Logger, *bytes.Buffer) {
buf := bytes.NewBuffer([]byte{})
log := logrus.New()
log.Formatter = &logrus.TextFormatter{}
log.Out = buf
log.Level = logrus.TraceLevel
return log, buf
}
func TestLogrusLoggerAdapter_no_fields(t *testing.T) {
log, buf := setup()
logger := NewLogrusLogger(log)
logger.Info("foo", watermill.LogFields{})
out := buf.String()
assert.Contains(t, out, `level=info msg=foo`)
}
func TestLogrusLoggerAdapter_field_with_space(t *testing.T) {
log, buf := setup()
logger := NewLogrusLogger(log)
logger.Info("foo", watermill.LogFields{"foo": `bar baz`})
out := buf.String()
assert.Contains(t, out, `level=info msg=foo foo="bar baz"`)
}
func TestLogrusLoggerAdapter_With_with_fields(t *testing.T) {
log, buf := setup()
baseLogger := NewLogrusLogger(log)
fieldsLogger := baseLogger.With(watermill.LogFields{"foo": "1"})
for msg, logger := range map[string]watermill.LoggerAdapter{"base": baseLogger, "with": fieldsLogger} {
logger.Error(msg, nil, watermill.LogFields{"bar": "2"})
logger.Info(msg, watermill.LogFields{"bar": "3"})
logger.Debug(msg, watermill.LogFields{"bar": "4"})
logger.Trace(msg, watermill.LogFields{"bar": "5"})
}
out := buf.String()
assert.Contains(t, out, `level=error msg=base bar=2 err="<nil>"`)
assert.Contains(t, out, `level=info msg=base bar=3`)
assert.Contains(t, out, `level=debug msg=base bar=4`)
assert.Contains(t, out, `level=trace msg=base bar=5`)
assert.Contains(t, out, `level=error msg=with bar=2 err="<nil>" foo=1`)
assert.Contains(t, out, `level=info msg=with bar=3 foo=1`)
assert.Contains(t, out, `level=debug msg=with bar=4 foo=1`)
assert.Contains(t, out, `level=trace msg=with bar=5 foo=1`)
}