-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglobal_test.go
143 lines (130 loc) · 4.09 KB
/
global_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
package wlog_test
import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/webitel/wlog"
)
func TestLoggingBeforeInitialized(t *testing.T) {
require.NotPanics(t, func() {
// None of these should segfault before wlog is globally configured
wlog.Info("info log")
wlog.Debug("debug log")
wlog.Warn("warning log")
wlog.Error("error log")
wlog.Critical("critical log")
})
}
func TestLoggingAfterInitialized(t *testing.T) {
testCases := []struct {
Description string
LoggerConfiguration *wlog.LoggerConfiguration
ExpectedLogs []string
}{
{
"file logging, json, debug",
&wlog.LoggerConfiguration{
EnableConsole: false,
EnableFile: true,
FileJson: true,
FileLevel: wlog.LevelDebug,
},
[]string{
`{"level":"debug","ts":0,"caller":"wlog/global_test.go:0","msg":"real debug log"}`,
`{"level":"info","ts":0,"caller":"wlog/global_test.go:0","msg":"real info log"}`,
`{"level":"warn","ts":0,"caller":"wlog/global_test.go:0","msg":"real warning log"}`,
`{"level":"error","ts":0,"caller":"wlog/global_test.go:0","msg":"real error log"}`,
`{"level":"error","ts":0,"caller":"wlog/global_test.go:0","msg":"real critical log"}`,
},
},
{
"file logging, json, error",
&wlog.LoggerConfiguration{
EnableConsole: false,
EnableFile: true,
FileJson: true,
FileLevel: wlog.LevelError,
},
[]string{
`{"level":"error","ts":0,"caller":"wlog/global_test.go:0","msg":"real error log"}`,
`{"level":"error","ts":0,"caller":"wlog/global_test.go:0","msg":"real critical log"}`,
},
},
{
"file logging, non-json, debug",
&wlog.LoggerConfiguration{
EnableConsole: false,
EnableFile: true,
FileJson: false,
FileLevel: wlog.LevelDebug,
},
[]string{
`TIME debug wlog/global_test.go:0 real debug log`,
`TIME info wlog/global_test.go:0 real info log`,
`TIME warn wlog/global_test.go:0 real warning log`,
`TIME error wlog/global_test.go:0 real error log`,
`TIME error wlog/global_test.go:0 real critical log`,
},
},
{
"file logging, non-json, error",
&wlog.LoggerConfiguration{
EnableConsole: false,
EnableFile: true,
FileJson: false,
FileLevel: wlog.LevelError,
},
[]string{
`TIME error wlog/global_test.go:0 real error log`,
`TIME error wlog/global_test.go:0 real critical log`,
},
},
}
for _, testCase := range testCases {
t.Run(testCase.Description, func(t *testing.T) {
var filePath string
if testCase.LoggerConfiguration.EnableFile {
tempDir, err := ioutil.TempDir(os.TempDir(), "TestLoggingAfterInitialized")
require.NoError(t, err)
defer os.Remove(tempDir)
filePath = filepath.Join(tempDir, "file.log")
testCase.LoggerConfiguration.FileLocation = filePath
}
logger := wlog.NewLogger(testCase.LoggerConfiguration)
wlog.InitGlobalLogger(logger)
wlog.Debug("real debug log")
wlog.Info("real info log")
wlog.Warn("real warning log")
wlog.Error("real error log")
wlog.Critical("real critical log")
if testCase.LoggerConfiguration.EnableFile {
logs, err := ioutil.ReadFile(filePath)
require.NoError(t, err)
actual := strings.TrimSpace(string(logs))
if testCase.LoggerConfiguration.FileJson {
reTs := regexp.MustCompile(`"ts":[0-9\.]+`)
reCaller := regexp.MustCompile(`"caller":"([^"]+):[0-9\.]+"`)
actual = reTs.ReplaceAllString(actual, `"ts":0`)
actual = reCaller.ReplaceAllString(actual, `"caller":"$1:0"`)
} else {
actualRows := strings.Split(actual, "\n")
for i, actualRow := range actualRows {
actualFields := strings.Split(actualRow, "\t")
if len(actualFields) > 3 {
actualFields[0] = "TIME"
reCaller := regexp.MustCompile(`([^"]+):[0-9\.]+`)
actualFields[2] = reCaller.ReplaceAllString(actualFields[2], "$1:0")
actualRows[i] = strings.Join(actualFields, "\t")
}
}
actual = strings.Join(actualRows, "\n")
}
require.Equal(t, testCase.ExpectedLogs, strings.Split(actual, "\n"))
}
})
}
}