-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Panic on wrong number of key value pairs #896
Comments
I wrote a static analysis tool to catch this today, because I've also been bitten by it: https://github.com/sethvargo/zapw |
Thank you for the report, agreed that this should not cause a panic, and instead add a field indicating that there's a missing argument, similar to |
@prashantv I think removing the panic would be considered a breaking API change. The panic is clearly documented and people may be depending on that behavior. |
Hello! We discussed this a little and although we agree that it would be a behavioral Another data point here is that we don't expect people to depend on panics as That said, we're happy to reconsider this if there's a good argument to keep |
@leopucci I tried to reproduce the panic, but it looks like this test case covers putting a non-string value in as a key Lines 92 to 97 in a68efdb
Are you configuring the logger, err := zap.NewProduction() Can you provide an example which will cause a panic please? |
Hey @dherbst , I just changed my job so I don´t have access to the git repository to access the commit and give you the exact situation. I do have some lines of the old code that could help you narrow down.
Something near this, but without some key or value. Maybe it is related to size. 3 or 4 pairs. try messing on something like this. I Hope it helps Pucci |
I cannot reproduce the reported problem when using Here is my attempt at a reproduction: package main
import (
"fmt"
"go.uber.org/zap"
)
func main() {
fmt.Println("starting")
// logger, err := zap.NewProduction() // does not panic
logger, err := zap.NewDevelopment()
if err != nil {
fmt.Println("err", err)
}
defer logger.Sync()
sugar := logger.Sugar()
sugar.Warnw("message", "this", "that")
sugar.Warnw("message", "this", "that", 2)
fmt.Println("done")
} |
Some of our production code could be under debugging mode. |
Looking at the code, I'd also only expect panics in development mode (it uses I'd recommend against using the development logger in production given the behaviour of |
As a software pattern, it is common to have defined modes, development/production for instance. To me, a wrong key/par does not mean that the software needs to stop completely just for a wrong log entry. I am aligned with @abhinav view, of a less restrictive mode, and that panic is a catastrophic violation approach, that does not make sense for a wrong log entry. Even in Development mode. |
Hello.
I agree with @abhinav 's point of view. And, some people may use |
Given that there is unexpected usage of the development mode in production, it does seem safer not to panic. We're aligned on changing the behaviour here to avoid panics, and welcome any PRs fixing this, thanks! |
SugaredLogger should not cause panics in user code. For keys without matching values or non-string keys, instead of panicking, log an error and move on. Resolves #896
I forgot to add a key to a value on
func (s *SugaredLogger) Warn(msg string, keysAndValues ...interface{}) {
And it caused me a panic crash of the app.
Shouldn´t be wise to protect this error from happening?
The code compiles normally, you deploy on production... it stays there for months.. then the log is printed and the system crashes because a log was wrong. (not my case just hypothetical.)
This is a thing that can be protected. We always have the human factor to account for.
zap/sugar.go
Line 264 in a68efdb
The text was updated successfully, but these errors were encountered: