-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_test.go
executable file
·154 lines (133 loc) · 3.55 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
147
148
149
150
151
152
153
154
package utils
import (
"bytes"
"io/ioutil"
"os"
"regexp"
"testing"
)
var logFile = "test.log"
func cleanup() {
os.Remove(logFile)
}
func TestMain(m *testing.M) {
code := m.Run()
cleanup()
os.Exit(code)
}
func TestLoggerWithFile(t *testing.T) {
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
t.Fatal(err)
}
logger := NewLogger(file, LogLevel.DEBUG)
if err != nil {
t.Fatal(err)
}
logger.Debug("hello")
b, err := ioutil.ReadFile(logFile)
if err != nil {
t.Fatal(err)
}
dump := bytes.NewBuffer(b).String()
if !regexp.MustCompile("hello").MatchString(dump) {
t.Errorf("trace message not found in string:\n%s", dump)
}
}
func TestPrint(t *testing.T) {
var b bytes.Buffer
logger := NewLogger(&b, LogLevel.TRACE)
logger.Trace("print this message")
dump := b.String()
if !regexp.MustCompile("print this message").MatchString(dump) {
t.Errorf("trace message not found in string:\n%s", dump)
}
}
func TestTrace(t *testing.T) {
var b bytes.Buffer
logger := NewLogger(&b, LogLevel.TRACE)
logger.Trace("some trace message")
dump := b.String()
if !regexp.MustCompile("some trace message").MatchString(dump) {
t.Errorf("trace message not found in string:\n%s", dump)
}
}
func TestDebug(t *testing.T) {
var b bytes.Buffer
logger := NewLogger(&b, LogLevel.DEBUG)
logger.Debug("some debug message")
dump := b.String()
if !regexp.MustCompile("some debug message").MatchString(dump) {
t.Errorf("debug message not found in string:\n%s", dump)
}
}
func TestInfo(t *testing.T) {
var b bytes.Buffer
logger := NewLogger(&b, LogLevel.INFO)
logger.Info("some info message")
dump := b.String()
if !regexp.MustCompile("some info message").MatchString(dump) {
t.Errorf("info message not found in string:\n%s", dump)
}
}
func TestWarn(t *testing.T) {
var b bytes.Buffer
logger := NewLogger(&b, LogLevel.WARN)
logger.Warn("some warn message")
dump := b.String()
if !regexp.MustCompile("some warn message").MatchString(dump) {
t.Errorf("warn message not found in string:\n%s", dump)
}
}
func TestError(t *testing.T) {
var b bytes.Buffer
logger := NewLogger(&b, LogLevel.ERROR)
logger.Error("some error message")
dump := b.String()
if !regexp.MustCompile("some error message").MatchString(dump) {
t.Errorf("error message not found in string:\n%s", dump)
}
}
func TestFatal(t *testing.T) {
var b bytes.Buffer
logger := NewLogger(&b, LogLevel.FATAL)
logger.Fatal("some fatal message")
dump := b.String()
if !regexp.MustCompile("some fatal message").MatchString(dump) {
t.Errorf("fatal message not found in string:\n%s", dump)
}
}
func TestLogLevel(t *testing.T) {
var b bytes.Buffer
logger := NewLogger(&b, LogLevel.TRACE)
logger.Level = LogLevel.DEBUG
logger.Trace("trace me!")
dump := b.String()
if regexp.MustCompile("trace me!").MatchString(dump) {
t.Errorf("expected empty; got %s", dump)
}
logger.Level = LogLevel.INFO
logger.Debug("debugging")
dump = b.String()
if regexp.MustCompile("debugging").MatchString(dump) {
t.Errorf("expected empty; got %s", dump)
}
logger.Level = LogLevel.WARN
logger.Info("some info")
dump = b.String()
if regexp.MustCompile("some info").MatchString(dump) {
t.Errorf("expected empty; got %s", dump)
}
logger.Level = LogLevel.ERROR
logger.Warn("warning!")
dump = b.String()
if regexp.MustCompile("warning!").MatchString(dump) {
t.Errorf("expected empty; got %s", dump)
}
logger.Level = LogLevel.FATAL
logger.Error("some error")
dump = b.String()
if regexp.MustCompile("some error").MatchString(dump) {
t.Errorf("expected empty; got %s", dump)
}
}