forked from icco/logrus-stackdriver-formatter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger_test.go
75 lines (58 loc) · 1.83 KB
/
logger_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
package logadapter
import (
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
type mockLogrusLogger struct{}
func (l *mockLogrusLogger) GetLevel() logrus.Level {
return logrus.DebugLevel
}
func (l *mockLogrusLogger) WithFields(fields logrus.Fields) *logrus.Entry {
return &logrus.Entry{}
}
func TestLogrusGoKitLogger_extractLogElements_basic(t *testing.T) {
mockLogrus := &mockLogrusLogger{}
logger := &LogrusGoKitLogger{mockLogrus}
fields, level, msg := logger.extractLogElements(
"msg", "testy mctestface",
"level", "error",
"foo", "bar",
"number", 42,
"flag", true)
expectedFields := logrus.Fields{}
expectedFields["foo"] = "bar"
expectedFields["number"] = 42
expectedFields["flag"] = true
assert.Equal(t, expectedFields, fields)
assert.Equal(t, logrus.ErrorLevel, level)
assert.Equal(t, "testy mctestface", msg)
}
func TestLogrusGoKitLogger_extractLogElements_defaultLevel(t *testing.T) {
mockLogrus := &mockLogrusLogger{}
logger := &LogrusGoKitLogger{mockLogrus}
fields, level, msg := logger.extractLogElements("msg", "testy mctestface")
expectedFields := logrus.Fields{}
assert.Equal(t, expectedFields, fields)
assert.Equal(t, logrus.DebugLevel, level)
assert.Equal(t, "testy mctestface", msg)
}
func TestLogrusGoKitLogger_extractLogElements_errorOverride(t *testing.T) {
mockLogrus := &mockLogrusLogger{}
logger := &LogrusGoKitLogger{mockLogrus}
fields, level, msg := logger.extractLogElements(
"err", "test error",
"msg", "some message",
"level", "debug",
"number", 42,
"flag", true,
)
expectedFields := logrus.Fields{}
expectedFields["msg"] = "some message"
expectedFields["level"] = "debug"
expectedFields["number"] = 42
expectedFields["flag"] = true
assert.Equal(t, expectedFields, fields)
assert.Equal(t, logrus.ErrorLevel, level)
assert.Equal(t, "test error", msg)
}