-
Notifications
You must be signed in to change notification settings - Fork 654
Expand file tree
/
Copy pathlogger_test.go
More file actions
125 lines (104 loc) · 3.02 KB
/
logger_test.go
File metadata and controls
125 lines (104 loc) · 3.02 KB
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
package clickhouse
import (
"bytes"
"log/slog"
"strings"
"testing"
)
// TestLegacyDebugfBackwardCompatibility tests that the old Debug/Debugf options still work
func TestLegacyDebugfBackwardCompatibility(t *testing.T) {
var buf bytes.Buffer
called := false
opt := &Options{
Debug: true,
Debugf: func(format string, v ...any) {
called = true
buf.WriteString("LEGACY: ")
buf.WriteString(format)
},
}
logger := opt.logger()
logger.Debug("test message")
if !called {
t.Error("Legacy Debugf was not called")
}
if !strings.Contains(buf.String(), "LEGACY:") {
t.Error("Legacy Debugf did not write expected prefix")
}
}
// TestNewLoggerOption tests the new Logger option with slog
func TestNewLoggerOption(t *testing.T) {
var buf bytes.Buffer
handler := slog.NewTextHandler(&buf, &slog.HandlerOptions{
Level: slog.LevelDebug,
})
customLogger := slog.New(handler)
opt := &Options{
Logger: customLogger,
}
logger := opt.logger()
logger.Debug("test message", slog.String("key", "value"))
output := buf.String()
if !strings.Contains(output, "test message") {
t.Error("Expected message not found in log output")
}
if !strings.Contains(output, "key=value") {
t.Error("Expected structured field not found in log output")
}
}
// TestNoopLoggerDefault tests that noop logger is used when no logging is configured
func TestNoopLoggerDefault(t *testing.T) {
opt := &Options{}
logger := opt.logger()
// Should not panic with noop logger
logger.Debug("test message")
logger.Info("test message")
logger.Warn("test message")
logger.Error("test message")
}
// TestLegacyDebugfPriority tests that legacy Debugf takes priority over new Logger
func TestLegacyDebugfPriority(t *testing.T) {
var buf bytes.Buffer
legacyCalled := false
handler := slog.NewTextHandler(&buf, &slog.HandlerOptions{
Level: slog.LevelDebug,
})
customLogger := slog.New(handler)
opt := &Options{
Debug: true,
Debugf: func(format string, v ...any) {
legacyCalled = true
},
Logger: customLogger,
}
logger := opt.logger()
logger.Debug("test message")
// Legacy Debugf should take priority
if !legacyCalled {
t.Error("Legacy Debugf should take priority but was not called")
}
// Buffer should be empty since legacy Debugf was used instead
if buf.Len() > 0 {
t.Error("New Logger should not be used when legacy Debugf is set")
}
}
// TestPrepareConnLogger tests the connection logger enrichment
func TestPrepareConnLogger(t *testing.T) {
var buf bytes.Buffer
handler := slog.NewTextHandler(&buf, &slog.HandlerOptions{
Level: slog.LevelDebug,
})
baseLogger := slog.New(handler)
connLogger := prepareConnLogger(baseLogger, 123, "localhost:9000", "native")
connLogger.Debug("connection established")
output := buf.String()
if !strings.Contains(output, "conn_id=123") {
t.Error("Expected conn_id in log output")
}
if !strings.Contains(output, "remote_addr=localhost:9000") {
t.Error("Expected remote_addr in log output")
}
if !strings.Contains(output, "protocol=native") {
t.Error("Expected protocol in log output")
}
}