@@ -19,7 +19,9 @@ package zap
19
19
import (
20
20
"bytes"
21
21
"encoding/json"
22
+ "flag"
22
23
"io/ioutil"
24
+ "os"
23
25
24
26
"github.com/go-logr/logr"
25
27
. "github.com/onsi/ginkgo"
@@ -296,3 +298,92 @@ var _ = Describe("Zap logger setup", func() {
296
298
})
297
299
})
298
300
})
301
+
302
+ var _ = Describe ("Zap log level flag options setup" , func () {
303
+ var (
304
+ fromFlags Options
305
+ fs flag.FlagSet
306
+ )
307
+
308
+ BeforeEach (func () {
309
+ fromFlags = Options {}
310
+ fs = * flag .NewFlagSet (os .Args [0 ], flag .ExitOnError ) //flags are now reset
311
+ })
312
+
313
+ Context ("with zap-log-level options provided" , func () {
314
+ It ("Should set info/debug zap-log-level flags." , func () {
315
+ args := []string {"--zap-log-level=debug" }
316
+ fromFlags .BindFlags (& fs )
317
+ if err := fs .Parse (args ); err != nil {
318
+ Expect (err ).ToNot (HaveOccurred ())
319
+ }
320
+ logOut := new (bytes.Buffer )
321
+
322
+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
323
+ logger .V (0 ).Info ("info text" )
324
+ logger .V (1 ).Info ("debug 1 text" )
325
+
326
+ outRaw := logOut .Bytes ()
327
+
328
+ Expect (string (outRaw )).Should (ContainSubstring ("info text" ))
329
+ Expect (string (outRaw )).Should (ContainSubstring ("debug 1 text" ))
330
+
331
+ })
332
+
333
+ It ("Should set error zap-log-level flags." , func () {
334
+ args := []string {"--zap-log-level=error" }
335
+ fromFlags .BindFlags (& fs )
336
+ if err := fs .Parse (args ); err != nil {
337
+ Expect (err ).ToNot (HaveOccurred ())
338
+ }
339
+ logOut := new (bytes.Buffer )
340
+
341
+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
342
+ logger .V (0 ).Info ("info text" )
343
+ logger .V (1 ).Info ("debug 1 text" )
344
+
345
+ outRaw := logOut .Bytes ()
346
+
347
+ Expect (outRaw ).To (BeEmpty ())
348
+ })
349
+
350
+ It ("Should set level 1 debug zap-log-level flags." , func () {
351
+ args := []string {"--zap-log-level=1" }
352
+ fromFlags .BindFlags (& fs )
353
+ if err := fs .Parse (args ); err != nil {
354
+ Expect (err ).ToNot (HaveOccurred ())
355
+ }
356
+ logOut := new (bytes.Buffer )
357
+
358
+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
359
+ logger .V (0 ).Info ("info text" )
360
+ logger .V (1 ).Info ("debug 1 text" )
361
+
362
+ outRaw := logOut .Bytes ()
363
+
364
+ Expect (string (outRaw )).Should (ContainSubstring ("info text" ))
365
+ Expect (string (outRaw )).Should (ContainSubstring ("debug 1 text" ))
366
+ })
367
+
368
+ It ("Should set Level 2 increased verbosity for zap-log-level flags." , func () {
369
+ args := []string {"--zap-log-level=2" }
370
+ fromFlags .BindFlags (& fs )
371
+ if err := fs .Parse (args ); err != nil {
372
+ Expect (err ).ToNot (HaveOccurred ())
373
+ }
374
+ logOut := new (bytes.Buffer )
375
+
376
+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
377
+ logger .V (0 ).Info ("info text" )
378
+ logger .V (1 ).Info ("debug 1 text" )
379
+ logger .V (2 ).Info ("debug 2 text" )
380
+
381
+ outRaw := logOut .Bytes ()
382
+
383
+ Expect (string (outRaw )).Should (ContainSubstring ("info text" ))
384
+ Expect (string (outRaw )).Should (ContainSubstring ("debug 1 text" ))
385
+ Expect (string (outRaw )).Should (ContainSubstring ("debug 2 text" ))
386
+
387
+ })
388
+ })
389
+ })
0 commit comments