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

Alternate, map-based sugared logger #247

Closed
wants to merge 31 commits into from
Closed

Alternate, map-based sugared logger #247

wants to merge 31 commits into from

Conversation

akshayjshah
Copy link
Contributor

@akshayjshah akshayjshah commented Jan 16, 2017

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 and string to pass messages.

This avoids allocating to pass strings as interface{} (for both the message and the field keys). In our BenchmarkZapSugarAddingFields 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.

BenchmarkLogrusAddingFields-4                         	  200000	      9601 ns/op	    5727 B/op	      77 allocs/op
BenchmarkLogrusWithAccumulatedContext-4               	  200000	      7622 ns/op	    3919 B/op	      61 allocs/op
BenchmarkLogrusWithoutFields-4                        	 1000000	      2222 ns/op	    1321 B/op	      25 allocs/op

BenchmarkZapAddingFields-4                            	 1000000	      1260 ns/op	     768 B/op	       4 allocs/op
BenchmarkZapWithAccumulatedContext-4                  	 5000000	       325 ns/op	      64 B/op	       2 allocs/op
BenchmarkZapWithoutFields-4                           	 5000000	       339 ns/op	      64 B/op	       2 allocs/op

BenchmarkZapSugarAddingFields-4                       	  500000	      2924 ns/op	    1729 B/op	      15 allocs/op
BenchmarkZapSugarWithAccumulatedContext-4             	 5000000	       370 ns/op	      64 B/op	       2 allocs/op
BenchmarkZapSugarWithoutFields-4                      	 5000000	       378 ns/op	      64 B/op	       2 allocs/op

mikluko and others added 30 commits January 13, 2017 15:33
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{}`.
@mention-bot
Copy link

@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.

@prashantv
Copy link
Collaborator

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 Ctx map, or they can pass a bunch of field/values. Then the API for each level should only need:

Panic(msg string, ctx... interface{})
Panicf(fmt string, args... interface{})

Copy link
Contributor

@jcorbin jcorbin left a 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).

@prashantv prashantv mentioned this pull request Jan 17, 2017
@akshayjshah
Copy link
Contributor Author

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.

@akshayjshah akshayjshah deleted the ajs-sugar-maps branch January 19, 2017 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants