-
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
Alternate, map-based sugared logger #247
Conversation
The sugared logger benchmarks were artificially good because we weren't logging anything :/
Switch from a varargs-based approach to sugaring to a map-based approach. This avoids allocations to convert strings to `interface{}`.
@akshayjshah, thanks for your PR! By analyzing the history of the files in this pull request, we identified @jcorbin, @prashantv and @pravj to be potential reviewers. |
That is a lot of methods, I don't think we want that large of an API. How about an approach that is more similar to log15, where you always take varargs, and the user can choose to pass a single Panic(msg string, ctx... interface{})
Panicf(fmt string, args... interface{}) |
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.
I agree that this doesn't seem worth it to me, but the biggest problem I see is that the caller skip isn't right:
- Debug calls log calls Check.Write, needing +1 skip
- DebugWith calls log ... needing +1 skip
- DebugF calls Debug calls log ... needing +2 skip
We currently don't have support for dynamically varied caller skip, and cannot without moving caller annotation out of logger.check
(e.g. defer it until Check.Write time, when we have access to the []Field
).
Fixed the caller skip - totally an oversight on my part. Don't think that this requires a facility for dynamic skips; the current situation is, IMO, simple and quite tolerable. |
This PR is a sibling to #185; only one of the two should be merged. It explores a different sugaring API, using
map[string]interface{}
to pass fields andstring
to pass messages.This avoids allocating to pass strings as
interface{}
(for both the message and the field keys). In ourBenchmarkZapSugarAddingFields
benchmark, this saves roughly 1 microsecond and 20 small allocations at each log site. However, it actually allocates more memory overall, increases verbosity at log sites, decreases flexibility, and necessitates at least seven additional methods on the sugared logger.IMO, this isn't a good tradeoff.