This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlogger_test.go
165 lines (146 loc) · 3.63 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
155
156
157
158
159
160
161
162
163
164
165
package log
import (
"bytes"
"strings"
"sync"
"testing"
"time"
)
func TestNormalizeTopic(t *testing.T) {
t.Parallel()
if normalizeTopic("abc.-def") != "abc.-def" {
t.Error("abc.-def")
}
if normalizeTopic("Abc.-Def") != "abc.-def" {
t.Error("Abc.-Def")
}
if normalizeTopic("Abc._Def") != "abc.-def" {
t.Error("Abc._Def")
}
}
func TestLogger(t *testing.T) {
t.Parallel()
l := NewLogger()
if l.Topic() != "log.test" {
t.Error(`topic != "log.test"`)
}
l.SetTopic("hoge")
if l.Topic() != "hoge" {
t.Error("failed to set topic")
}
buf := new(bytes.Buffer)
l.SetOutput(buf)
l.SetFormatter(Logfmt{})
if err := l.Debug("hoge", nil); err != nil {
t.Error(err)
}
if buf.Len() != 0 {
t.Error("debug log should be ignored")
}
l.SetThreshold(LvDebug)
if err := l.Debug("hoge", nil); err != nil {
t.Error(err)
}
if buf.Len() == 0 {
t.Error("debug log should not be ignored")
} else {
s := buf.String()
if !strings.Contains(s, "topic=hoge") {
t.Error("Invalid log: " + s)
}
}
if err := l.SetThresholdByName("hoge"); err == nil {
t.Error("hoge must not be a valid log level")
}
if l.SetThresholdByName("critical"); l.Threshold() != LvCritical {
t.Error("Failed to set threshold as critical")
}
if l.SetThresholdByName("crit"); l.Threshold() != LvCritical {
t.Error("Failed to set threshold as critical")
}
if l.SetThresholdByName("debug"); l.Threshold() != LvDebug {
t.Error("Failed to set threshold as debug")
}
l.SetDefaults(map[string]interface{}{
FnSecret: true,
})
buf.Reset()
if err := l.Debug("hoge", nil); err != nil {
t.Error(err)
} else {
s := buf.String()
if !strings.Contains(s, `secret=true`) {
t.Error("failed to include default fields")
}
}
buf.Reset()
fields := map[string]interface{}{
FnSecret: true,
"custom": 10000,
}
if err := l.Debug("hoge", fields); err != nil {
t.Error(err)
} else {
s := buf.String()
if !strings.Contains(s, `secret=true`) {
t.Error("failed to specify fields")
}
if !strings.Contains(s, `custom=10000`) {
t.Error("failed to specify custom field")
}
}
}
type testFormat struct {
}
func (f testFormat) String() string {
return "test"
}
func (f testFormat) Format(buf []byte, l *Logger, t time.Time, severity int, msg string, fields map[string]interface{}) ([]byte, error) {
return []byte(msg), nil
}
func TestWriter(t *testing.T) {
l := NewLogger()
output := new(bytes.Buffer)
l.SetOutput(output)
l.SetFormatter(testFormat{})
w := l.Writer(LvCritical)
cases := []struct {
Arg, Expected []byte
}{
{[]byte("hogehoge\nhoge"), []byte("hogehoge")},
{[]byte("foofoo\nfoo"), []byte("hogefoofoo")},
{[]byte("barbar\nbar"), []byte("foobarbar")},
}
for _, tc := range cases {
_, err := w.Write(tc.Arg)
if err != nil {
t.Fatal(err)
}
actual := output.Bytes()
if !bytes.Equal(actual, tc.Expected) {
t.Errorf("actual: %s, expected: %s", string(actual), string(tc.Expected))
}
output.Reset()
}
}
func TestWriteThrough(t *testing.T) {
logger := NewLogger()
buf := new(bytes.Buffer)
logger.SetOutput(buf)
abc := []byte("abc\ndef\n")
var wg sync.WaitGroup
wg.Add(8)
go func() { logger.Error("hoge", nil); wg.Done() }()
go func() { logger.Error("hoge", nil); wg.Done() }()
go func() { logger.Error("hoge", nil); wg.Done() }()
go func() { logger.Error("hoge", nil); wg.Done() }()
go func() { logger.WriteThrough(abc); wg.Done() }()
go func() { logger.Error("hoge", nil); wg.Done() }()
go func() { logger.Error("hoge", nil); wg.Done() }()
go func() { logger.Error("hoge", nil); wg.Done() }()
wg.Wait()
data := buf.Bytes()
if !bytes.Contains(data, abc) {
t.Error(`!bytes.Contains(data, []byte("abc\ndef\n"))`)
}
}