-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger_test.go
146 lines (109 loc) · 3.09 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package logbuch
import (
"bytes"
"os"
"strings"
"testing"
)
func TestLoggerLog(t *testing.T) {
var buffer bytes.Buffer
expect := "Hello World!\n"
logger := NewLogger(&buffer, &buffer)
logger.Debug("Hello %s!", "World")
if !strings.Contains(buffer.String(), expect) {
t.Fatalf("Expected '%v' to contain '%v'", buffer.String(), expect)
}
logger.Debug("Another log %s", "entry")
expect = "Another log entry\n"
if !strings.Contains(buffer.String(), expect) {
t.Fatalf("Expected '%v' to contain '%v'", buffer.String(), expect)
}
}
func TestLoggerLevel(t *testing.T) {
logger := NewLogger(nil, nil)
logger.SetLevel(LevelDebug)
if logger.level != LevelDebug || logger.GetLevel() != LevelDebug {
t.Fatal("Unexpected log level")
}
logger.SetLevel(LevelInfo)
if logger.level != LevelInfo || logger.GetLevel() != LevelInfo {
t.Fatal("Unexpected log level")
}
logger.SetLevel(LevelWarning)
if logger.level != LevelWarning || logger.GetLevel() != LevelWarning {
t.Fatal("Unexpected log level")
}
logger.SetLevel(LevelError)
if logger.level != LevelError || logger.GetLevel() != LevelError {
t.Fatal("Unexpected log level")
}
}
func TestLoggerFormatterAndOutput(t *testing.T) {
var buffer bytes.Buffer
expect := "Hello World!\n"
logger := NewLogger(&buffer, &buffer)
logger.Debug("Hello %s!", "World")
if !strings.Contains(buffer.String(), expect) {
t.Fatalf("Expected '%v' to contain '%v'", buffer.String(), expect)
}
formatter := NewDiscardFormatter()
logger.SetFormatter(formatter)
if logger.GetFormatter() != formatter {
t.Fatal("Unexpected formatter")
}
var newBuffer bytes.Buffer
logger.SetOut(LevelDebug, &newBuffer)
logger.Debug("Another log %s", "entry")
if newBuffer.String() != "" {
t.Fatalf("Expected log to be empty but was: %v", buffer.String())
}
}
func TestLoggerFatal(t *testing.T) {
var stderr bytes.Buffer
defer func() {
if r := recover(); r == nil {
t.Fatalf("Fatal must panic")
}
if !strings.Contains(stderr.String(), "Fatal message") {
t.Fatalf("Log must contain error message")
}
}()
logger := NewLogger(nil, &stderr)
logger.Fatal("Fatal %v", "message")
}
func TestNewLoggerRollingFileAppender(t *testing.T) {
if err := os.RemoveAll("out"); err != nil {
t.Fatal(err)
}
stdRFA, err := NewRollingFileAppender(0, 0, 0, "out", &testNameSchema{name: ".std"})
if err != nil {
t.Fatal(err)
}
errRFA, err := NewRollingFileAppender(0, 0, 0, "out", &testNameSchema{name: ".err"})
if err != nil {
t.Fatal(err)
}
logger := NewLogger(stdRFA, errRFA)
logger.Info("info")
logger.Error("error")
if err := stdRFA.Close(); err != nil {
t.Fatal(err)
}
if err := errRFA.Close(); err != nil {
t.Fatal(err)
}
stdFile, err := os.ReadFile("out/1_log.std.txt")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(stdFile), "info") {
t.Fatalf("Info log must contain log output, but was: %v", string(stdFile))
}
errFile, err := os.ReadFile("out/1_log.err.txt")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(errFile), "error") {
t.Fatalf("Error log must contain log output, but was: %v", string(errFile))
}
}