-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[v1, 4/8] Replace NewJSON constructor with NewLogger #115
Conversation
Since loggers and encoders are now completely separate, make the main constructor `NewLogger` and have it take an encoder as its first argument. This requires changing many call sites.
Did you consider this alternate interface:
This makes creating a new default logger a fair bit more verbose: zap.NewLogger(zap.NewJSONEncoder(), [..]) vs zap.New([...]) And in the case that someone wants to customize the encoder, it's not very complicated either: zap.New(zap.Encoder(zap.NewJSONEncoder()), [...]) |
I considered that (and roughed out some code), but ended up rejecting it for two reasons:
|
@@ -86,7 +86,12 @@ func BenchmarkZapDisabledLevelsWithoutFields(b *testing.B) { | |||
|
|||
func BenchmarkZapDisabledLevelsAccumulatedContext(b *testing.B) { | |||
context := fakeFields() | |||
logger := zap.NewJSON(zap.ErrorLevel, zap.Output(zap.Discard), zap.Fields(context...)) | |||
logger := zap.NewLogger( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: might be worth adding a helper to create a new JSON logger with output discarded, that takes a list of additional options on top.
Makes sense. lgtm |
Will change |
Make the logger type name more generic, since it's not actually JSON-specific, and make the logger constructor take an encoder implementation. Since we're injecting encoders, we can stop using the ugly
StubTime
hack (which we'll remove altogether in the next PR).This is the fourth of eight PRs to finish up the large pre-v1.0.0 refactoring:
NewLogger
constructor and removeNewJSON
StubTime
Encoder.AddFields
(since we already haveField.AddTo
)The remaining items in the v1 milestone shouldn't require large-scale changes.