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

SugaredLogger treats []byte fields as []uint8 #335

Closed
ansel1 opened this issue Feb 24, 2017 · 4 comments
Closed

SugaredLogger treats []byte fields as []uint8 #335

ansel1 opened this issue Feb 24, 2017 · 4 comments
Labels

Comments

@ansel1
Copy link
Contributor

ansel1 commented Feb 24, 2017

SugaredLogger turns all field values into zap.Any() fields. Unfortunately, zap.Any("key", []byte("value")) maps the []byte value to a zap.Uint8() field rather than a zap.Binary() field (because in golang, byte is just an alias for uint8)

It's all technically correct, but it leads to very unintuitive default behavior with SugaredLogger (and arguable, with zap.Any()).

A type switch cannot have a case for both []byte and []uint8, because they are just aliases of one another: the compiler won't allow it. But arguably, assuming the caller intended []byte will probably be correct more often that assuming the caller intended []uint8.

@akshayjshah
Copy link
Contributor

Good point - I didn't think carefully enough about this when I wrote Any. Are you willing to open a PR that fixes this?

Also, we'll run into the same situation with []rune and []int32. I think that the current behavior of preferring integers is more intuitive there, so let's leave that as-is.

In case you weren't aware, also keep in mind that the sugared logger accepts strongly-typed Field structs too. If you need to get unblocked immediately, you can always

sugar.Infow("Failed to unmarshal JSON.", zap.Binary("raw", someByteSlice), "error", unmarshalingErr)

@ansel1
Copy link
Contributor Author

ansel1 commented Feb 24, 2017

sure, I can do that. So the fix plan is just swap out the []uint8 case in zap.Any() for a []byte case, plus update tests. on it.

@akshayjshah
Copy link
Contributor

Yep, exactly. Let's also update the documentation comment for Any to clarify that we treat []byte and []uint8 as binary and []rune as []int32.

@akshayjshah
Copy link
Contributor

Landed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants