@@ -19,11 +19,14 @@ package zap
1919import (
2020 "bytes"
2121 "encoding/json"
22+ "flag"
2223 "io/ioutil"
24+ "os"
2325
2426 "github.com/go-logr/logr"
2527 . "github.com/onsi/ginkgo"
2628 . "github.com/onsi/gomega"
29+ "go.uber.org/zap/zapcore"
2730 kapi "k8s.io/api/core/v1"
2831 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2932 "k8s.io/apimachinery/pkg/types"
@@ -296,3 +299,164 @@ var _ = Describe("Zap logger setup", func() {
296299 })
297300 })
298301})
302+
303+ var _ = Describe ("Zap log level flag options setup" , func () {
304+ var (
305+ fromFlags Options
306+ fs flag.FlagSet
307+ logInfoLevel0 = "info text"
308+ logDebugLevel1 = "debug 1 text"
309+ logDebugLevel2 = "debug 2 text"
310+ logDebugLevel3 = "debug 3 text"
311+ )
312+
313+ BeforeEach (func () {
314+ fromFlags = Options {}
315+ fs = * flag .NewFlagSet (os .Args [0 ], flag .ExitOnError )
316+ })
317+
318+ Context ("with zap-log-level options provided" , func () {
319+ It ("Should output logs for info and debug zap-log-level." , func () {
320+ args := []string {"--zap-log-level=debug" }
321+ fromFlags .BindFlags (& fs )
322+ err := fs .Parse (args )
323+ Expect (err ).ToNot (HaveOccurred ())
324+ logOut := new (bytes.Buffer )
325+
326+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
327+ logger .V (0 ).Info (logInfoLevel0 )
328+ logger .V (1 ).Info (logDebugLevel1 )
329+
330+ outRaw := logOut .Bytes ()
331+
332+ Expect (string (outRaw )).Should (ContainSubstring (logInfoLevel0 ))
333+ Expect (string (outRaw )).Should (ContainSubstring (logDebugLevel1 ))
334+
335+ })
336+
337+ It ("Should output only error logs, otherwise empty logs" , func () {
338+ args := []string {"--zap-log-level=error" }
339+ fromFlags .BindFlags (& fs )
340+ err := fs .Parse (args )
341+ Expect (err ).ToNot (HaveOccurred ())
342+
343+ logOut := new (bytes.Buffer )
344+
345+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
346+ logger .V (0 ).Info (logInfoLevel0 )
347+ logger .V (1 ).Info (logDebugLevel1 )
348+
349+ outRaw := logOut .Bytes ()
350+
351+ Expect (outRaw ).To (BeEmpty ())
352+ })
353+
354+ })
355+
356+ Context ("with zap-log-level with increased verbosity." , func () {
357+ It ("Should output debug and info log, with default production mode." , func () {
358+ args := []string {"--zap-log-level=1" }
359+ fromFlags .BindFlags (& fs )
360+ err := fs .Parse (args )
361+ Expect (err ).ToNot (HaveOccurred ())
362+ logOut := new (bytes.Buffer )
363+
364+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
365+ logger .V (0 ).Info (logInfoLevel0 )
366+ logger .V (1 ).Info (logDebugLevel1 )
367+
368+ outRaw := logOut .Bytes ()
369+
370+ Expect (string (outRaw )).Should (ContainSubstring (logInfoLevel0 ))
371+ Expect (string (outRaw )).Should (ContainSubstring (logDebugLevel1 ))
372+ })
373+
374+ It ("Should output info and debug logs, with development mode." , func () {
375+ args := []string {"--zap-log-level=1" , "--zap-devel=true" }
376+ fromFlags .BindFlags (& fs )
377+ err := fs .Parse (args )
378+ Expect (err ).ToNot (HaveOccurred ())
379+ logOut := new (bytes.Buffer )
380+
381+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
382+ logger .V (0 ).Info (logInfoLevel0 )
383+ logger .V (1 ).Info (logDebugLevel1 )
384+
385+ outRaw := logOut .Bytes ()
386+
387+ Expect (string (outRaw )).Should (ContainSubstring (logInfoLevel0 ))
388+ Expect (string (outRaw )).Should (ContainSubstring (logDebugLevel1 ))
389+ })
390+
391+ It ("Should output info, and debug logs with increased verbosity, and with development mode set to true." , func () {
392+ args := []string {"--zap-log-level=3" , "--zap-devel=false" }
393+ fromFlags .BindFlags (& fs )
394+ err := fs .Parse (args )
395+ Expect (err ).ToNot (HaveOccurred ())
396+ logOut := new (bytes.Buffer )
397+
398+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
399+ logger .V (0 ).Info (logInfoLevel0 )
400+ logger .V (1 ).Info (logDebugLevel1 )
401+ logger .V (2 ).Info (logDebugLevel2 )
402+ logger .V (3 ).Info (logDebugLevel3 )
403+
404+ outRaw := logOut .Bytes ()
405+
406+ Expect (string (outRaw )).Should (ContainSubstring (logInfoLevel0 ))
407+ Expect (string (outRaw )).Should (ContainSubstring (logDebugLevel1 ))
408+ Expect (string (outRaw )).Should (ContainSubstring (logDebugLevel2 ))
409+ Expect (string (outRaw )).Should (ContainSubstring (logDebugLevel3 ))
410+
411+ })
412+ It ("Should output info, and debug logs with increased verbosity, and with production mode set to true." , func () {
413+ args := []string {"--zap-log-level=3" , "--zap-devel=true" }
414+ fromFlags .BindFlags (& fs )
415+ err := fs .Parse (args )
416+ Expect (err ).ToNot (HaveOccurred ())
417+ logOut := new (bytes.Buffer )
418+
419+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
420+ logger .V (0 ).Info (logInfoLevel0 )
421+ logger .V (1 ).Info (logDebugLevel1 )
422+ logger .V (2 ).Info (logDebugLevel2 )
423+ logger .V (3 ).Info (logDebugLevel3 )
424+
425+ outRaw := logOut .Bytes ()
426+
427+ Expect (string (outRaw )).Should (ContainSubstring (logInfoLevel0 ))
428+ Expect (string (outRaw )).Should (ContainSubstring (logDebugLevel1 ))
429+ Expect (string (outRaw )).Should (ContainSubstring (logDebugLevel2 ))
430+ Expect (string (outRaw )).Should (ContainSubstring (logDebugLevel3 ))
431+
432+ })
433+
434+ })
435+
436+ Context ("with zap-stacktrace-level options provided" , func () {
437+
438+ It ("Should output stacktrace at info level, with development mode set to true." , func () {
439+ args := []string {"--zap-stacktrace-level=info" , "--zap-devel=true" }
440+ fromFlags .BindFlags (& fs )
441+ err := fs .Parse (args )
442+ Expect (err ).ToNot (HaveOccurred ())
443+ out := Options {}
444+ UseFlagOptions (& fromFlags )(& out )
445+
446+ Expect (out .StacktraceLevel .Enabled (zapcore .InfoLevel )).To (BeTrue ())
447+ })
448+
449+ It ("Should output stacktrace at error level, with development mode set to true." , func () {
450+ args := []string {"--zap-stacktrace-level=error" , "--zap-devel=true" }
451+ fromFlags .BindFlags (& fs )
452+ err := fs .Parse (args )
453+ Expect (err ).ToNot (HaveOccurred ())
454+ out := Options {}
455+ UseFlagOptions (& fromFlags )(& out )
456+
457+ Expect (out .StacktraceLevel .Enabled (zapcore .ErrorLevel )).To (BeTrue ())
458+ })
459+
460+ })
461+
462+ })
0 commit comments