@@ -19,11 +19,14 @@ 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"
26
28
. "github.com/onsi/gomega"
29
+ "go.uber.org/zap/zapcore"
27
30
kapi "k8s.io/api/core/v1"
28
31
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29
32
"k8s.io/apimachinery/pkg/types"
@@ -296,3 +299,135 @@ var _ = Describe("Zap logger setup", func() {
296
299
})
297
300
})
298
301
})
302
+
303
+ var _ = Describe ("Zap log level flag options setup" , func () {
304
+ var (
305
+ fromFlags Options
306
+ fs flag.FlagSet
307
+ )
308
+
309
+ BeforeEach (func () {
310
+ fromFlags = Options {}
311
+ fs = * flag .NewFlagSet (os .Args [0 ], flag .ExitOnError ) //flags are now reset
312
+ })
313
+
314
+ Context ("with zap-log-level options provided" , func () {
315
+ It ("Should set info/debug zap-log-level flags." , func () {
316
+ args := []string {"--zap-log-level=debug" }
317
+ fromFlags .BindFlags (& fs )
318
+ if err := fs .Parse (args ); err != nil {
319
+ Expect (err ).ToNot (HaveOccurred ())
320
+ }
321
+ logOut := new (bytes.Buffer )
322
+
323
+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
324
+ logger .V (0 ).Info ("info text" )
325
+ logger .V (1 ).Info ("debug 1 text" )
326
+
327
+ outRaw := logOut .Bytes ()
328
+
329
+ Expect (string (outRaw )).Should (ContainSubstring ("info text" ))
330
+ Expect (string (outRaw )).Should (ContainSubstring ("debug 1 text" ))
331
+
332
+ })
333
+
334
+ It ("Should set error zap-log-level flags." , func () {
335
+ args := []string {"--zap-log-level=error" }
336
+ fromFlags .BindFlags (& fs )
337
+ if err := fs .Parse (args ); err != nil {
338
+ Expect (err ).ToNot (HaveOccurred ())
339
+ }
340
+ logOut := new (bytes.Buffer )
341
+
342
+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
343
+ logger .V (0 ).Info ("info text" )
344
+ logger .V (1 ).Info ("debug 1 text" )
345
+
346
+ outRaw := logOut .Bytes ()
347
+
348
+ Expect (outRaw ).To (BeEmpty ())
349
+ })
350
+
351
+ It ("Should set level 1 debug zap-log-level flags." , func () {
352
+ args := []string {"--zap-log-level=1" }
353
+ fromFlags .BindFlags (& fs )
354
+ if err := fs .Parse (args ); err != nil {
355
+ Expect (err ).ToNot (HaveOccurred ())
356
+ }
357
+ logOut := new (bytes.Buffer )
358
+
359
+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
360
+ logger .V (0 ).Info ("info text" )
361
+ logger .V (1 ).Info ("debug 1 text" )
362
+
363
+ outRaw := logOut .Bytes ()
364
+
365
+ Expect (string (outRaw )).Should (ContainSubstring ("info text" ))
366
+ Expect (string (outRaw )).Should (ContainSubstring ("debug 1 text" ))
367
+ })
368
+ })
369
+
370
+ Context ("with zap-log-level with increased verbosity." , func () {
371
+ It ("Should set Level 2 increased verbosity for zap-log-level flags." , func () {
372
+ args := []string {"--zap-log-level=2" }
373
+ fromFlags .BindFlags (& fs )
374
+ if err := fs .Parse (args ); err != nil {
375
+ Expect (err ).ToNot (HaveOccurred ())
376
+ }
377
+ logOut := new (bytes.Buffer )
378
+
379
+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
380
+ logger .V (0 ).Info ("info text" )
381
+ logger .V (1 ).Info ("debug 1 text" )
382
+ logger .V (2 ).Info ("debug 2 text" )
383
+
384
+ outRaw := logOut .Bytes ()
385
+
386
+ Expect (string (outRaw )).Should (ContainSubstring ("info text" ))
387
+ Expect (string (outRaw )).Should (ContainSubstring ("debug 1 text" ))
388
+ Expect (string (outRaw )).Should (ContainSubstring ("debug 2 text" ))
389
+
390
+ })
391
+
392
+ })
393
+
394
+ Context ("with zap-stacktrace-level options provided" , func () {
395
+
396
+ It ("Should set info stacktrace for zap-stacktrace-level flags." , func () {
397
+ args := []string {"--zap-stacktrace-level=info" , "--zap-devel=true" }
398
+ fromFlags .BindFlags (& fs )
399
+ if err := fs .Parse (args ); err != nil {
400
+ Expect (err ).ToNot (HaveOccurred ())
401
+ }
402
+ out := Options {}
403
+ UseFlagOptions (& fromFlags )(& out )
404
+
405
+ Expect (out .StacktraceLevel .Enabled (zapcore .InfoLevel )).To (BeTrue ())
406
+ })
407
+
408
+ It ("Should set error stacktrace for zap-stacktrace-level flags." , func () {
409
+ args := []string {"--zap-stacktrace-level=error" , "--zap-devel=true" }
410
+ fromFlags .BindFlags (& fs )
411
+ if err := fs .Parse (args ); err != nil {
412
+ Expect (err ).ToNot (HaveOccurred ())
413
+ }
414
+ out := Options {}
415
+ UseFlagOptions (& fromFlags )(& out )
416
+
417
+ Expect (out .StacktraceLevel .Enabled (zapcore .ErrorLevel )).To (BeTrue ())
418
+ })
419
+
420
+ It ("Should disable stacktrace for zap-stacktrace-level flags." , func () {
421
+ args := []string {"--zap-stacktrace-level=disabled" , "--zap-devel=true" }
422
+ fromFlags .BindFlags (& fs )
423
+ if err := fs .Parse (args ); err != nil {
424
+ Expect (err ).ToNot (HaveOccurred ())
425
+ }
426
+ out := Options {}
427
+ UseFlagOptions (& fromFlags )(& out )
428
+
429
+ Expect (out .StacktraceLevel .Enabled (zapcore .FatalLevel )).To (BeTrue ())
430
+ })
431
+ })
432
+
433
+ })
0 commit comments