-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_test.go
122 lines (99 loc) · 2.74 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
package corelog
import (
"context"
"errors"
"fmt"
"log/slog"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewLogger(t *testing.T) {
logger := NewLogger("test")
assert.Equal(t, "test", logger.name)
}
func TestLoggerLogWithConfigOverride(t *testing.T) {
SetConfig(Config{
Level: LevelError,
Format: FormatJSON,
Output: OutputStderr,
EnableStackTrace: false,
EnableSource: false,
})
SetConfigOverride("test", Config{
Level: LevelInfo,
Format: FormatText,
Output: OutputStdout,
EnableStackTrace: true,
EnableSource: true,
})
handler := &TestHandler{}
logger := NewLogger("test")
logger.handler = handler
ctx := context.Background()
err := errors.New("test error")
logger.log(ctx, slog.LevelInfo, err, "test", []slog.Attr{slog.Any("arg1", "val1")})
require.Len(t, handler.records, 1)
assert.NotEqual(t, uintptr(0x00), handler.records[0].PC)
assert.Equal(t, slog.LevelInfo, handler.records[0].Level)
assert.Equal(t, "test", handler.records[0].Message)
attrs := []slog.Attr{
slog.Any(nameKey, "test"),
slog.Any("arg1", "val1"),
slog.Any(stackKey, fmt.Sprintf("%+v", err)),
slog.Any(errorKey, err.Error()),
}
assertRecordAttrs(t, handler.records[0], attrs...)
}
func TestLoggerInfoWithEnableSource(t *testing.T) {
SetConfig(Config{EnableSource: true})
handler := &TestHandler{}
logger := &Logger{
name: "test",
handler: handler,
}
logger.Info("test", String("arg1", "val1"))
require.Len(t, handler.records, 1)
assert.Equal(t, slog.LevelInfo, handler.records[0].Level)
assert.Equal(t, "test", handler.records[0].Message)
assert.NotEqual(t, uintptr(0x00), handler.records[0].PC)
attrs := []slog.Attr{
slog.Any(nameKey, "test"),
slog.Any("arg1", "val1"),
}
assertRecordAttrs(t, handler.records[0], attrs...)
}
func TestLoggerWithAttrs(t *testing.T) {
handler := &TestHandler{}
logger := &Logger{
handler: handler,
}
attrs := []slog.Attr{slog.Any("extra", "value")}
other := logger.WithAttrs(attrs...)
otherHandler, ok := other.handler.(*TestHandler)
require.True(t, ok)
assert.Equal(t, attrs, otherHandler.attrs)
}
func TestLoggerWithGroup(t *testing.T) {
handler := &TestHandler{}
logger := &Logger{
handler: handler,
}
other := logger.WithGroup("group")
otherHandler, ok := other.handler.(*TestHandler)
require.True(t, ok)
assert.Equal(t, "group", otherHandler.group)
}
// assertRecordAttrs asserts that the record has matching attributes.
func assertRecordAttrs(
t *testing.T,
record slog.Record,
expected ...slog.Attr,
) {
var actual []slog.Attr
record.Attrs(func(a slog.Attr) bool {
actual = append(actual, a)
return true
})
assert.Equal(t, expected, actual)
}