-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlog_adapter_test.go
174 lines (148 loc) · 4.36 KB
/
xlog_adapter_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
// 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/xconf/blob/main/LICENSE.
package xconf_test
import (
"errors"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/actforgood/xconf"
"github.com/actforgood/xlog"
)
//nolint:lll
func ExampleLogLevelProvider() {
const logLevelKey = "APP_LOG_LEVEL"
const defaultLogLevel = "WARN"
// initialize Config object,
// loader can be any other Loader, used this for the sake of simplicity and readability.
loader := xconf.PlainLoader(map[string]any{
logLevelKey: "INFO",
})
config, _ := xconf.NewDefaultConfig( // treat the error on live code!
loader,
xconf.DefaultConfigWithReloadInterval(time.Second),
)
defer config.Close()
// initialize the logger with min level taken from Config.
opts := xlog.NewCommonOpts()
opts.MinLevel = xconf.LogLevelProvider(config, logLevelKey, defaultLogLevel, opts.LevelLabels)
opts.Time = func() any { // mock time for output check
return "2022-06-21T17:17:20Z"
}
opts.Source = xlog.SourceProvider(4, 1) // keep only filename for output check
logger := xlog.NewSyncLogger(
os.Stdout,
xlog.SyncLoggerWithOptions(opts),
)
defer logger.Close()
logger.Info(xlog.MessageKey, "log level is taken from xconf.Config")
logger.Debug(xlog.MessageKey, "this message should not end up being logged as min level is INFO")
// Output:
// {"date":"2022-06-21T17:17:20Z","lvl":"INFO","msg":"log level is taken from xconf.Config","src":"/xlog_adapter_test.go:49"}
}
func ExampleLogErrorHandler() {
// initialize the logger.
logger := xlog.NewSyncLogger(os.Stdout)
defer logger.Close()
// initialize Config object,
// loader can be any other Loader, used this for the sake of simplicity and readability.
loader := xconf.PlainLoader(map[string]any{
"foo": "bar",
})
loggerGetter := func() xlog.Logger { return logger }
config, _ := xconf.NewDefaultConfig( // treat the error on live code!
loader,
xconf.DefaultConfigWithReloadInterval(time.Second),
xconf.DefaultConfigWithReloadErrorHandler(xconf.LogErrorHandler(loggerGetter)),
)
defer config.Close()
foo := config.Get("foo", "default foo").(string)
fmt.Println(foo)
// Output:
// bar
}
func TestLogLevelProvider(t *testing.T) {
t.Parallel()
t.Run("level key config is found", testLogLevelProviderWithExistingKey)
t.Run("level key config is not found - default", testLogLevelProviderWithDefaultLevel)
}
func testLogLevelProviderWithExistingKey(t *testing.T) {
t.Parallel()
// arrange
var (
loader = xconf.PlainLoader(map[string]any{
"APP_LOG_LEVEL": "DEBUG",
"foo": "bar",
})
config, _ = xconf.NewDefaultConfig(loader)
loggerCommOpts = xlog.NewCommonOpts()
subject = xconf.LogLevelProvider(
config,
"APP_LOG_LEVEL",
"INFO",
loggerCommOpts.LevelLabels,
)
expectedResult = xlog.LevelDebug
)
for i := 0; i < 10; i++ {
// act
result := subject()
// assert
assertEqual(t, expectedResult, result)
}
}
func testLogLevelProviderWithDefaultLevel(t *testing.T) {
t.Parallel()
// arrange
var (
loader = xconf.PlainLoader(map[string]any{
"foo": "bar",
})
config, _ = xconf.NewDefaultConfig(loader)
loggerCommOpts = xlog.NewCommonOpts()
subject = xconf.LogLevelProvider(
config,
"APP_LOG_LEVEL",
"INFO",
loggerCommOpts.LevelLabels,
)
expectedResult = xlog.LevelInfo
)
for i := 0; i < 10; i++ {
// act
result := subject()
// assert
assertEqual(t, expectedResult, result)
}
}
func TestLogErrorHandler(t *testing.T) {
t.Parallel()
// arrange
var (
logger = xlog.NewMockLogger()
loggerGetter = func() xlog.Logger { return logger }
subject = xconf.LogErrorHandler(loggerGetter)
err = errors.New("reload test error")
)
defer logger.Close()
logger.SetLogCallback(xlog.LevelError, func(keyValues ...any) {
if assertEqual(t, 4, len(keyValues)) {
assertEqual(t, xlog.MessageKey, keyValues[0])
if msg, ok := keyValues[1].(string); assertTrue(t, ok) {
assertTrue(t, strings.Contains(msg, "could not reload configuration"))
}
assertEqual(t, xlog.ErrorKey, keyValues[2])
if errMsg, ok := keyValues[3].(string); assertTrue(t, ok) {
assertTrue(t, strings.Contains(errMsg, err.Error()))
}
}
})
// act
subject(err)
// assert
assertEqual(t, 1, logger.LogCallsCount(xlog.LevelError))
}