8
8
"bytes"
9
9
"context"
10
10
"errors"
11
- "strings"
12
11
"testing"
13
12
"time"
14
13
)
@@ -27,8 +26,6 @@ func (h *mockFailingHandler) Handle(ctx context.Context, r Record) error {
27
26
}
28
27
29
28
func TestMultiHandler (t * testing.T ) {
30
- ctx := context .Background ()
31
-
32
29
t .Run ("Handle sends log to all handlers" , func (t * testing.T ) {
33
30
var buf1 , buf2 bytes.Buffer
34
31
h1 := NewTextHandler (& buf1 , nil )
@@ -39,21 +36,8 @@ func TestMultiHandler(t *testing.T) {
39
36
40
37
logger .Info ("hello world" , "user" , "test" )
41
38
42
- // Check the output of the Text handler.
43
- output1 := buf1 .String ()
44
- if ! strings .Contains (output1 , `level=INFO` ) ||
45
- ! strings .Contains (output1 , `msg="hello world"` ) ||
46
- ! strings .Contains (output1 , `user=test` ) {
47
- t .Errorf ("Text handler did not receive the correct log message. Got: %s" , output1 )
48
- }
49
-
50
- // Check the output of the JSON handle.
51
- output2 := buf2 .String ()
52
- if ! strings .Contains (output2 , `"level":"INFO"` ) ||
53
- ! strings .Contains (output2 , `"msg":"hello world"` ) ||
54
- ! strings .Contains (output2 , `"user":"test"` ) {
55
- t .Errorf ("JSON handler did not receive the correct log message. Got: %s" , output2 )
56
- }
39
+ checkLogOutput (t , buf1 .String (), "time=" + textTimeRE + ` level=INFO msg="hello world" user=test` )
40
+ checkLogOutput (t , buf2 .String (), `{"time":"` + jsonTimeRE + `","level":"INFO","msg":"hello world","user":"test"}` )
57
41
})
58
42
59
43
t .Run ("Enabled returns true if any handler is enabled" , func (t * testing.T ) {
@@ -62,10 +46,10 @@ func TestMultiHandler(t *testing.T) {
62
46
63
47
multi := MultiHandler (h1 , h2 )
64
48
65
- if ! multi .Enabled (ctx , LevelInfo ) {
49
+ if ! multi .Enabled (context . Background () , LevelInfo ) {
66
50
t .Error ("Enabled should be true for INFO level, but got false" )
67
51
}
68
- if ! multi .Enabled (ctx , LevelError ) {
52
+ if ! multi .Enabled (context . Background () , LevelError ) {
69
53
t .Error ("Enabled should be true for ERROR level, but got false" )
70
54
}
71
55
})
@@ -76,7 +60,7 @@ func TestMultiHandler(t *testing.T) {
76
60
77
61
multi := MultiHandler (h1 , h2 )
78
62
79
- if multi .Enabled (ctx , LevelDebug ) {
63
+ if multi .Enabled (context . Background () , LevelDebug ) {
80
64
t .Error ("Enabled should be false for DEBUG level, but got true" )
81
65
}
82
66
})
@@ -91,15 +75,8 @@ func TestMultiHandler(t *testing.T) {
91
75
92
76
logger .Info ("request processed" )
93
77
94
- // Check if the Text handler contains the attribute.
95
- if ! strings .Contains (buf1 .String (), "request_id=123" ) {
96
- t .Errorf ("Text handler output missing attribute. Got: %s" , buf1 .String ())
97
- }
98
-
99
- // Check if the JSON handler contains the attribute.
100
- if ! strings .Contains (buf2 .String (), `"request_id":"123"` ) {
101
- t .Errorf ("JSON handler output missing attribute. Got: %s" , buf2 .String ())
102
- }
78
+ checkLogOutput (t , buf1 .String (), "time=" + textTimeRE + ` level=INFO msg="request processed" request_id=123` )
79
+ checkLogOutput (t , buf2 .String (), `{"time":"` + jsonTimeRE + `","level":"INFO","msg":"request processed","request_id":"123"}` )
103
80
})
104
81
105
82
t .Run ("WithGroup propagates group to all handlers" , func (t * testing.T ) {
@@ -112,24 +89,14 @@ func TestMultiHandler(t *testing.T) {
112
89
113
90
logger .Info ("user login" , "user_id" , 42 )
114
91
115
- // Check if the Text handler contains the group.
116
- expectedText := "req.user_id=42"
117
- if ! strings .Contains (buf1 .String (), expectedText ) {
118
- t .Errorf ("Text handler output missing group. Expected to contain %q, Got: %s" , expectedText , buf1 .String ())
119
- }
120
-
121
- // Check if the JSON handler contains the group.
122
- expectedJSON := `"req":{"user_id":42}`
123
- if ! strings .Contains (buf2 .String (), expectedJSON ) {
124
- t .Errorf ("JSON handler output missing group. Expected to contain %q, Got: %s" , expectedJSON , buf2 .String ())
125
- }
92
+ checkLogOutput (t , buf1 .String (), "time=" + textTimeRE + ` level=INFO msg="user login" req.user_id=42` )
93
+ checkLogOutput (t , buf2 .String (), `{"time":"` + jsonTimeRE + `","level":"INFO","msg":"user login","req":{"user_id":42}}` )
126
94
})
127
95
128
96
t .Run ("Handle propagates errors from handlers" , func (t * testing.T ) {
129
97
var buf bytes.Buffer
130
98
h1 := NewTextHandler (& buf , nil )
131
99
132
- // Simulate a handler that will fail.
133
100
errFail := errors .New ("fake fail" )
134
101
h2 := & mockFailingHandler {
135
102
Handler : NewTextHandler (& bytes.Buffer {}, nil ),
@@ -138,32 +105,21 @@ func TestMultiHandler(t *testing.T) {
138
105
139
106
multi := MultiHandler (h1 , h2 )
140
107
141
- err := multi .Handle (ctx , NewRecord (time .Now (), LevelInfo , "test message" , 0 ))
142
-
143
- // Check if the error was returned correctly.
144
- if err == nil {
145
- t .Fatal ("Expected an error from Handle, but got nil" )
146
- }
108
+ err := multi .Handle (context .Background (), NewRecord (time .Now (), LevelInfo , "test message" , 0 ))
147
109
if ! errors .Is (err , errFail ) {
148
110
t .Errorf ("Expected error: %v, but got: %v" , errFail , err )
149
111
}
150
112
151
- // Also, check that the successful handler still output the log.
152
- if ! strings .Contains (buf .String (), "test message" ) {
153
- t .Error ("The successful handler should still have processed the log" )
154
- }
113
+ checkLogOutput (t , buf .String (), "time=" + textTimeRE + ` level=INFO msg="test message"` )
155
114
})
156
115
157
116
t .Run ("Handle with no handlers" , func (t * testing.T ) {
158
- // Create an empty multi-handler.
159
117
multi := MultiHandler ()
160
118
logger := New (multi )
161
119
162
- // This should be safe to call and do nothing.
163
- logger .Info ("this is nothing" )
120
+ logger .Info ("nothing" )
164
121
165
- // Calling Handle directly should also be safe.
166
- err := multi .Handle (ctx , NewRecord (time .Now (), LevelInfo , "test" , 0 ))
122
+ err := multi .Handle (context .Background (), NewRecord (time .Now (), LevelInfo , "test" , 0 ))
167
123
if err != nil {
168
124
t .Errorf ("Handle with no sub-handlers should return nil, but got: %v" , err )
169
125
}
0 commit comments