@@ -23,18 +23,25 @@ package zap
23
23
import (
24
24
"bytes"
25
25
"fmt"
26
- "io/ioutil"
27
26
"os"
28
27
"strings"
29
28
"testing"
30
29
31
30
"github.com/stretchr/testify/assert"
32
31
)
33
32
34
- func withJSONLogger (t testing.TB , f func (* jsonLogger , func () []string ), fields ... Field ) {
33
+ func opts (opts ... Option ) []Option {
34
+ return opts
35
+ }
36
+
37
+ func withJSONLogger (t testing.TB , opts []Option , f func (* jsonLogger , func () []string )) {
35
38
sink := & bytes.Buffer {}
36
39
errSink := & bytes.Buffer {}
37
- jl := NewJSON (All , sink , errSink , fields ... )
40
+
41
+ allOpts := make ([]Option , 0 , 3 + len (opts ))
42
+ allOpts = append (allOpts , All , Output (sink ), ErrorOutput (errSink ))
43
+ allOpts = append (allOpts , opts ... )
44
+ jl := NewJSON (allOpts ... )
38
45
jl .StubTime ()
39
46
40
47
f (jl .(* jsonLogger ), func () []string { return strings .Split (sink .String (), "\n " ) })
@@ -56,16 +63,15 @@ func assertFields(t testing.TB, jl Logger, getOutput func() []string, expectedFi
56
63
}
57
64
58
65
func TestJSONLoggerSetLevel (t * testing.T ) {
59
- withJSONLogger (t , func (jl * jsonLogger , _ func () []string ) {
66
+ withJSONLogger (t , nil , func (jl * jsonLogger , _ func () []string ) {
60
67
assert .Equal (t , All , jl .Level (), "Unexpected initial level." )
61
68
jl .SetLevel (Debug )
62
69
assert .Equal (t , Debug , jl .Level (), "Unexpected level after SetLevel." )
63
70
})
64
71
}
65
72
66
73
func TestJSONLoggerEnabled (t * testing.T ) {
67
- withJSONLogger (t , func (jl * jsonLogger , _ func () []string ) {
68
- jl .SetLevel (Info )
74
+ withJSONLogger (t , opts (Info ), func (jl * jsonLogger , _ func () []string ) {
69
75
assert .False (t , jl .Enabled (Debug ), "Debug logs shouldn't be enabled at Info level." )
70
76
assert .True (t , jl .Enabled (Info ), "Info logs should be enabled at Info level." )
71
77
assert .True (t , jl .Enabled (Warn ), "Warn logs should be enabled at Info level." )
@@ -85,7 +91,7 @@ func TestJSONLoggerEnabled(t *testing.T) {
85
91
func TestJSONLoggerConcurrentLevelMutation (t * testing.T ) {
86
92
// Trigger races for non-atomic level mutations.
87
93
proceed := make (chan struct {})
88
- jl := NewJSON (Info , ioutil . Discard , ioutil . Discard )
94
+ jl := NewJSON ()
89
95
90
96
for i := 0 ; i < 50 ; i ++ {
91
97
go func (l Logger ) {
@@ -101,51 +107,53 @@ func TestJSONLoggerConcurrentLevelMutation(t *testing.T) {
101
107
}
102
108
103
109
func TestJSONLoggerInitialFields (t * testing.T ) {
104
- withJSONLogger (t , func (jl * jsonLogger , output func () []string ) {
110
+ fieldOpts := opts (Fields (Int ("foo" , 42 ), String ("bar" , "baz" )))
111
+ withJSONLogger (t , fieldOpts , func (jl * jsonLogger , output func () []string ) {
105
112
assertFields (t , jl , output , `{"foo":42,"bar":"baz"}` )
106
- }, Int ( "foo" , 42 ), String ( "bar" , "baz" ) )
113
+ })
107
114
}
108
115
109
116
func TestJSONLoggerWith (t * testing.T ) {
110
- withJSONLogger (t , func (jl * jsonLogger , output func () []string ) {
117
+ fieldOpts := opts (Fields (Int ("foo" , 42 )))
118
+ withJSONLogger (t , fieldOpts , func (jl * jsonLogger , output func () []string ) {
111
119
// Child loggers should have copy-on-write semantics, so two children
112
120
// shouldn't stomp on each other's fields or affect the parent's fields.
113
121
jl .With (String ("one" , "two" )).Debug ("" )
114
122
jl .With (String ("three" , "four" )).Debug ("" )
115
123
assertFields (t , jl , output , `{"foo":42,"one":"two"}` , `{"foo":42,"three":"four"}` , `{"foo":42}` )
116
- }, Int ( "foo" , 42 ) )
124
+ })
117
125
}
118
126
119
127
func TestJSONLoggerDebug (t * testing.T ) {
120
- withJSONLogger (t , func (jl * jsonLogger , output func () []string ) {
128
+ withJSONLogger (t , nil , func (jl * jsonLogger , output func () []string ) {
121
129
jl .Debug ("foo" )
122
130
assertMessage (t , "debug" , "foo" , output ()[0 ])
123
131
})
124
132
}
125
133
126
134
func TestJSONLoggerInfo (t * testing.T ) {
127
- withJSONLogger (t , func (jl * jsonLogger , output func () []string ) {
135
+ withJSONLogger (t , nil , func (jl * jsonLogger , output func () []string ) {
128
136
jl .Info ("foo" )
129
137
assertMessage (t , "info" , "foo" , output ()[0 ])
130
138
})
131
139
}
132
140
133
141
func TestJSONLoggerWarn (t * testing.T ) {
134
- withJSONLogger (t , func (jl * jsonLogger , output func () []string ) {
142
+ withJSONLogger (t , nil , func (jl * jsonLogger , output func () []string ) {
135
143
jl .Warn ("foo" )
136
144
assertMessage (t , "warn" , "foo" , output ()[0 ])
137
145
})
138
146
}
139
147
140
148
func TestJSONLoggerError (t * testing.T ) {
141
- withJSONLogger (t , func (jl * jsonLogger , output func () []string ) {
149
+ withJSONLogger (t , nil , func (jl * jsonLogger , output func () []string ) {
142
150
jl .Error ("foo" )
143
151
assertMessage (t , "error" , "foo" , output ()[0 ])
144
152
})
145
153
}
146
154
147
155
func TestJSONLoggerPanic (t * testing.T ) {
148
- withJSONLogger (t , func (jl * jsonLogger , output func () []string ) {
156
+ withJSONLogger (t , nil , func (jl * jsonLogger , output func () []string ) {
149
157
assert .Panics (t , func () {
150
158
jl .Panic ("foo" )
151
159
})
@@ -154,7 +162,7 @@ func TestJSONLoggerPanic(t *testing.T) {
154
162
}
155
163
156
164
func TestJSONLoggerNoOpsDisabledLevels (t * testing.T ) {
157
- withJSONLogger (t , func (jl * jsonLogger , output func () []string ) {
165
+ withJSONLogger (t , nil , func (jl * jsonLogger , output func () []string ) {
158
166
jl .SetLevel (Warn )
159
167
jl .Info ("silence!" )
160
168
assert .Equal (t , []string {"" }, output (), "Expected logging at a disabled level to produce no output." )
@@ -168,7 +176,7 @@ func TestJSONLoggerInternalErrorHandlingNoSink(t *testing.T) {
168
176
169
177
buf := & bytes.Buffer {}
170
178
171
- jl := NewJSON (All , buf , nil , Object ("user" , fakeUser {"fail" }))
179
+ jl := NewJSON (All , Output ( buf ), Fields ( Object ("user" , fakeUser {"fail" }) ))
172
180
jl .StubTime ()
173
181
output := func () []string { return strings .Split (buf .String (), "\n " ) }
174
182
@@ -183,7 +191,7 @@ func TestJSONLoggerInternalErrorHandlingWithSink(t *testing.T) {
183
191
buf := & bytes.Buffer {}
184
192
errBuf := & bytes.Buffer {}
185
193
186
- jl := NewJSON (All , buf , errBuf , Object ("user" , fakeUser {"fail" }))
194
+ jl := NewJSON (All , Output ( buf ), ErrorOutput ( errBuf ), Fields ( Object ("user" , fakeUser {"fail" }) ))
187
195
jl .StubTime ()
188
196
output := func () []string { return strings .Split (buf .String (), "\n " ) }
189
197
@@ -197,7 +205,7 @@ func TestJSONLoggerInternalErrorHandlingWithSink(t *testing.T) {
197
205
func TestJSONLoggerRuntimeLevelChange (t * testing.T ) {
198
206
// Test that changing a logger's level also changes the level of all
199
207
// ancestors and descendants.
200
- grandparent := NewJSON (Info , ioutil . Discard , ioutil . Discard , Int ("generation" , 1 ))
208
+ grandparent := NewJSON (Fields ( Int ("generation" , 1 ) ))
201
209
parent := grandparent .With (Int ("generation" , 2 ))
202
210
child := parent .With (Int ("generation" , 3 ))
203
211
0 commit comments