Skip to content

Commit

Permalink
Allow panic as an option to -zap-stacktrace-level"
Browse files Browse the repository at this point in the history
This allows stack traces to be disabled for all but panic log entries.

The Zap library doesn't have an option to completely disable stack
traces.
  • Loading branch information
croomes committed Jan 20, 2021
1 parent 73c52e8 commit 5d4651a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
3 changes: 1 addition & 2 deletions pkg/log/zap/flags.go
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/log/zap/zap.go
Expand Up @@ -230,7 +230,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
Expand Down Expand Up @@ -260,7 +260,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.
Expand Down
11 changes: 11 additions & 0 deletions pkg/log/zap/zap_test.go
Expand Up @@ -423,6 +423,17 @@ 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())
})

})

Context("with only -zap-devel flag provided", func() {
Expand Down

0 comments on commit 5d4651a

Please sign in to comment.