diff --git a/pkg/log/zap/flags.go b/pkg/log/zap/flags.go index 7a7b764f6f..3339655075 100644 --- a/pkg/log/zap/flags.go +++ b/pkg/log/zap/flags.go @@ -34,11 +34,10 @@ var levelStrings = map[string]zapcore.Level{ "error": zap.ErrorLevel, } -// TODO Add level to disable stacktraces. -// https://github.com/kubernetes-sigs/controller-runtime/issues/1035 var stackLevelStrings = map[string]zapcore.Level{ "info": zap.InfoLevel, "error": zap.ErrorLevel, + "panic": zap.PanicLevel, } type encoderFlag struct { diff --git a/pkg/log/zap/zap.go b/pkg/log/zap/zap.go index b77c28fd08..8aff63ee84 100644 --- a/pkg/log/zap/zap.go +++ b/pkg/log/zap/zap.go @@ -238,7 +238,7 @@ func NewRaw(opts ...Opts) *zap.Logger { // zap-encoder: Zap log encoding (one of 'json' or 'console') // zap-log-level: Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error', // or any integer value > 0 which corresponds to custom debug levels of increasing verbosity") -// zap-stacktrace-level: Zap Level at and above which stacktraces are captured (one of 'info' or 'error') +// zap-stacktrace-level: Zap Level at and above which stacktraces are captured (one of 'info', 'error' or 'panic') func (o *Options) BindFlags(fs *flag.FlagSet) { // Set Development mode value @@ -268,7 +268,7 @@ func (o *Options) BindFlags(fs *flag.FlagSet) { o.StacktraceLevel = fromFlag } fs.Var(&stackVal, "zap-stacktrace-level", - "Zap Level at and above which stacktraces are captured (one of 'info', 'error').") + "Zap Level at and above which stacktraces are captured (one of 'info', 'error', 'panic').") } // UseFlagOptions configures the logger to use the Options set by parsing zap option flags from the CLI. diff --git a/pkg/log/zap/zap_test.go b/pkg/log/zap/zap_test.go index aab781f97c..c5f24c38b5 100644 --- a/pkg/log/zap/zap_test.go +++ b/pkg/log/zap/zap_test.go @@ -423,6 +423,19 @@ var _ = Describe("Zap log level flag options setup", func() { Expect(out.StacktraceLevel.Enabled(zapcore.ErrorLevel)).To(BeTrue()) }) + It("Should output stacktrace at panic level, with development mode set to true.", func() { + args := []string{"--zap-stacktrace-level=panic", "--zap-devel=true"} + fromFlags.BindFlags(&fs) + err := fs.Parse(args) + Expect(err).ToNot(HaveOccurred()) + out := Options{} + UseFlagOptions(&fromFlags)(&out) + + Expect(out.StacktraceLevel.Enabled(zapcore.PanicLevel)).To(BeTrue()) + Expect(out.StacktraceLevel.Enabled(zapcore.ErrorLevel)).To(BeFalse()) + Expect(out.StacktraceLevel.Enabled(zapcore.InfoLevel)).To(BeFalse()) + }) + }) Context("with only -zap-devel flag provided", func() {