Skip to content

Commit

Permalink
Merge pull request #1348 from croomes/stack-trace-levels
Browse files Browse the repository at this point in the history
✨ Allow panic as an option to -zap-stacktrace-level"
  • Loading branch information
k8s-ci-robot committed Jan 22, 2021
2 parents 8144092 + 9b98559 commit 9e78e65
Show file tree
Hide file tree
Showing 3 changed files with 16 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 @@ -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
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions pkg/log/zap/zap_test.go
Expand Up @@ -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() {
Expand Down

0 comments on commit 9e78e65

Please sign in to comment.