-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_test.go
116 lines (82 loc) · 2.43 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package logger
import (
"bytes"
"context"
"testing"
"log/slog"
c "github.com/smartystreets/goconvey/convey"
)
func TestCreateLogger(t *testing.T) {
ctx := context.Background()
c.Convey("Logger initialization", t, func() {
c.Convey("For default (no level specified)", func() {
buf := &bytes.Buffer{}
logger := NewLogger(buf)
c.So(logger, c.ShouldNotBeNil)
c.Convey("Should log at ERROR level", func() {
logger.Debug("This log shouldn't appear")
logger.Error("This log should appear")
output := buf.String()
c.So(output, c.ShouldContainSubstring, "ERROR")
})
c.Convey("Should log FATAL properly", func() {
logger.Log(ctx, Fatal, "test")
output := buf.String()
c.So(output, c.ShouldContainSubstring, "FATAL")
c.So(output, c.ShouldContainSubstring, "test")
})
c.Convey("Unknown log level should default to error level", func() {
logger.Log(ctx, slog.Level(100), "unknown")
output := buf.String()
c.So(output, c.ShouldContainSubstring, "92")
c.So(output, c.ShouldContainSubstring, "unknown")
})
buf.Reset()
})
c.Convey("For DEBUG log level", func() {
buf := &bytes.Buffer{}
logger := NewLogger(buf, "DEBUG")
c.So(logger, c.ShouldNotBeNil)
logger.Debug("Debug message")
output := buf.String()
c.So(output, c.ShouldContainSubstring, "DEBUG")
buf.Reset()
})
c.Convey("For INFO log level", func() {
buf := &bytes.Buffer{}
logger := NewLogger(buf, "INFO")
c.So(logger, c.ShouldNotBeNil)
logger.Info("Info message")
output := buf.String()
c.So(output, c.ShouldContainSubstring, "INFO")
buf.Reset()
})
c.Convey("For WARN log level", func() {
buf := &bytes.Buffer{}
logger := NewLogger(buf, "WARN")
c.So(logger, c.ShouldNotBeNil)
logger.Warn("Warn message")
output := buf.String()
c.So(output, c.ShouldContainSubstring, "WARN")
buf.Reset()
})
c.Convey("For ERROR log level", func() {
buf := &bytes.Buffer{}
logger := NewLogger(buf, "ERROR")
c.So(logger, c.ShouldNotBeNil)
logger.Error("Error message")
output := buf.String()
c.So(output, c.ShouldContainSubstring, "ERROR")
buf.Reset()
})
c.Convey("For FATAL log level", func() {
buf := &bytes.Buffer{}
logger := NewLogger(buf, "FATAL")
c.So(logger, c.ShouldNotBeNil)
logger.Log(ctx, Fatal, "Fatal message")
output := buf.String()
c.So(output, c.ShouldContainSubstring, "FATAL")
buf.Reset()
})
})
}