-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
79 lines (59 loc) · 2.22 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
76
77
78
79
package context
import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
type logWriter struct {
logMessage string
}
func (writer *logWriter) Write(p []byte) (n int, err error) {
writer.logMessage = string(p)
return len(p), nil
}
func testLogMessage(t *testing.T, writer *logWriter, level string, message string) {
assert.True(t, strings.Contains(writer.logMessage, level))
assert.True(t, strings.Contains(writer.logMessage, message))
}
func TestSimpleLogger(t *testing.T) {
contextId := ContextId("test-context-id")
simpleLogger := NewSimpleLogger()
logWriter := &logWriter{}
simpleLogger.log.Out = logWriter
simpleLogger.log.Level = logrus.TraceLevel
simpleLogger.log.ExitFunc = func(i int) {
}
simpleLogger.Trace(contextId, "test message")
testLogMessage(t, logWriter, "TRACE", "test message")
simpleLogger.Debug(contextId, "test message")
testLogMessage(t, logWriter, "DEBUG", "test message")
simpleLogger.Error(contextId, "test message")
testLogMessage(t, logWriter, "ERROR", "test message")
simpleLogger.Info(contextId, "test message")
testLogMessage(t, logWriter, "INFO", "test message")
simpleLogger.Warning(contextId, "test message")
testLogMessage(t, logWriter, "WARNING", "test message")
simpleLogger.Fatal(contextId, "test message")
testLogMessage(t, logWriter, "FATAL", "test message")
assert.Panics(t, func() {
simpleLogger.Panic(contextId, "test message")
})
testLogMessage(t, logWriter, "PANIC", "test message")
simpleLogger.Tracef(contextId, "test message")
testLogMessage(t, logWriter, "TRACE", "test message")
simpleLogger.Debugf(contextId, "test message")
testLogMessage(t, logWriter, "DEBUG", "test message")
simpleLogger.Errorf(contextId, "test message")
testLogMessage(t, logWriter, "ERROR", "test message")
simpleLogger.Infof(contextId, "test message")
testLogMessage(t, logWriter, "INFO", "test message")
simpleLogger.Warningf(contextId, "test message")
testLogMessage(t, logWriter, "WARNING", "test message")
simpleLogger.Fatalf(contextId, "test message")
testLogMessage(t, logWriter, "FATAL", "test message")
assert.Panics(t, func() {
simpleLogger.Panicf(contextId, "test message")
})
testLogMessage(t, logWriter, "PANIC", "test message")
}