Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Allow panic as an option to -zap-stacktrace-level" #1348

Merged
merged 1 commit into from Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the more important test that its disabled on the next level, i.E. error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added disable check for info and error.

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