-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_multi_test.go
175 lines (151 loc) · 5.07 KB
/
logger_multi_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
166
167
168
169
170
171
172
173
174
175
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xlog/blob/main/LICENSE.
package xlog_test
import (
"errors"
"os"
"testing"
"github.com/actforgood/xlog"
)
func ExampleMultiLogger_splitMessagesByLevel() {
// In this example we create a (multi)logger that writes
// debug and info logs to standard output and
// warning, error and critical logs to error output.
errLgrOpts := xlog.NewCommonOpts()
errLgrOpts.MinLevel = xlog.FixedLevelProvider(xlog.LevelWarning)
errLgrOpts.Time = func() any { // mock time for output check
return "2022-03-20T16:01:20Z"
}
errLgrOpts.Source = xlog.SourceProvider(5, 1) // keep only filename for output check
errLgr := xlog.NewSyncLogger(
os.Stderr,
xlog.SyncLoggerWithOptions(errLgrOpts),
)
dbgLgrOpts := xlog.NewCommonOpts()
dbgLgrOpts.MinLevel = xlog.FixedLevelProvider(xlog.LevelDebug)
dbgLgrOpts.MaxLevel = xlog.FixedLevelProvider(xlog.LevelInfo)
dbgLgrOpts.Time = func() any { // mock time for output check
return "2022-03-20T16:01:20Z"
}
dbgLgrOpts.Source = xlog.SourceProvider(5, 1) // keep only filename for output check
dbgLgr := xlog.NewSyncLogger(
os.Stdout,
xlog.SyncLoggerWithOptions(dbgLgrOpts),
)
logger := xlog.NewMultiLogger(errLgr, dbgLgr)
defer logger.Close()
logger.Debug("msg", "I get written to standard output")
logger.Error("msg", "I get written to standard error")
// Output:
// {"date":"2022-03-20T16:01:20Z","lvl":"DEBUG","msg":"I get written to standard output","src":"/logger_multi_test.go:47"}
}
func ExampleMultiLogger_logToStdOutAndCustomFile() {
// In this example we create a (multi)logger that writes
// logs to standard output, and to a file.
stdOutLgrOpts := xlog.NewCommonOpts()
stdOutLgrOpts.MinLevel = xlog.FixedLevelProvider(xlog.LevelNone)
stdOutLgrOpts.Time = func() any { // mock time for output check
return "2022-03-15T16:01:20Z"
}
stdOutLgrOpts.Source = xlog.SourceProvider(5, 1) // keep only filename for output check
stdOutLgr := xlog.NewSyncLogger(
os.Stdout,
xlog.SyncLoggerWithOptions(stdOutLgrOpts),
)
fileLgrOpts := xlog.NewCommonOpts()
fileLgrOpts.MinLevel = xlog.FixedLevelProvider(xlog.LevelNone)
fileLgrOpts.Time = func() any { // mock time for output check
return "2022-03-15T16:01:20Z"
}
fileLgrOpts.Source = xlog.SourceProvider(5, 1) // keep only filename for output check
f, err := os.CreateTemp("", "x.log-*") // you will have your well known path defined
if err != nil {
panic(err)
}
fileLgr := xlog.NewSyncLogger(
f,
xlog.SyncLoggerWithOptions(fileLgrOpts),
)
logger := xlog.NewMultiLogger(stdOutLgr, fileLgr)
defer func() {
_ = logger.Close()
_ = f.Close()
_ = os.Remove(f.Name()) // you won't remove the file
}()
logger.Debug("msg", "I get written to standard output and to a file")
// Output:
// {"date":"2022-03-15T16:01:20Z","lvl":"DEBUG","msg":"I get written to standard output and to a file","src":"/logger_multi_test.go:91"}
}
func TestMultiLogger_logsOnEveryLogger(t *testing.T) {
t.Parallel()
// arrange
var (
levels = []xlog.Level{
xlog.LevelNone,
xlog.LevelDebug,
xlog.LevelInfo,
xlog.LevelWarning,
xlog.LevelError,
xlog.LevelCritical,
}
loggers = []xlog.Logger{
xlog.NewMockLogger(),
xlog.NewMockLogger(),
xlog.NewMockLogger(),
}
subject xlog.Logger = xlog.NewMultiLogger(loggers...)
kv = getInputKeyValues()
)
defer func() {
assertNil(t, subject.Close())
}()
for _, lvl := range levels {
for _, logger := range loggers {
lgr := logger.(*xlog.MockLogger)
lgr.SetLogCallback(lvl, func(keyValues ...any) {
assertEqual(t, kv, keyValues)
})
}
// act
callMethodByLevel(subject, lvl)
// assert
for _, logger := range loggers {
lgr := logger.(*xlog.MockLogger)
assertEqual(t, 1, lgr.LogCallsCount(lvl))
}
}
}
func TestMultiLogger_Close_closesAllLoggers(t *testing.T) {
t.Parallel()
// arrange
var (
logger1 = xlog.NewMockLogger()
logger2 = xlog.NewMockLogger()
logger3 = xlog.NewMockLogger()
loggers = []xlog.Logger{logger1, logger2, logger3}
subject = xlog.NewMultiLogger(loggers...)
expectedLogger1Err = errors.New("intentionally triggered logger 1 Close error")
expectedLogger3Err = errors.New("intentionally triggered logger 3 Close error")
)
logger1.SetCloseError(expectedLogger1Err)
logger3.SetCloseError(expectedLogger3Err)
// act
err := subject.Close()
// assert
if assertNotNil(t, err) {
assertTrue(t, errors.Is(err, expectedLogger1Err))
assertTrue(t, errors.Is(err, expectedLogger3Err))
}
for _, logger := range loggers {
lgr := logger.(*xlog.MockLogger)
assertEqual(t, 1, lgr.CloseCallsCount())
assertEqual(t, 0, lgr.LogCallsCount(xlog.LevelNone))
assertEqual(t, 0, lgr.LogCallsCount(xlog.LevelDebug))
assertEqual(t, 0, lgr.LogCallsCount(xlog.LevelInfo))
assertEqual(t, 0, lgr.LogCallsCount(xlog.LevelWarning))
assertEqual(t, 0, lgr.LogCallsCount(xlog.LevelError))
assertEqual(t, 0, lgr.LogCallsCount(xlog.LevelCritical))
}
}