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

Add finer grained caller annotation support #227

Merged
merged 14 commits into from
Jan 10, 2017
Merged

Add finer grained caller annotation support #227

merged 14 commits into from
Jan 10, 2017

Conversation

jcorbin
Copy link
Contributor

@jcorbin jcorbin commented Jan 4, 2017

@akshayjshah let me know if you're okay with adding the new field support or not; if so I'll write tests, if not we'll just stop at the new option for now.

Towards #40

@mention-bot
Copy link

@jcorbin, thanks for your PR! By analyzing the history of the files in this pull request, we identified @akshayjshah, @tamird and @noxiouz to be potential reviewers.

@akshayjshah akshayjshah changed the base branch from master to dev January 5, 2017 00:45
Copy link
Contributor

@akshayjshah akshayjshah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline - don't think we want to model "add the caller to every log message" as a field. Separately, we can add a field that adds caller info from n frames up.

@jcorbin jcorbin force-pushed the jcorbin.callerskip branch from 322fb2d to e891704 Compare January 5, 2017 18:50
@jcorbin
Copy link
Contributor Author

jcorbin commented Jan 5, 2017

Okay, dropped the field, and added a test for the new option. N.B. how I avoided coupling to line numbers by moving utility code into a new file; if you'd rather just use line numbers, we can go back to bb641cd.

@jcorbin jcorbin force-pushed the jcorbin.callerskip branch from 7148461 to 0e5f259 Compare January 6, 2017 20:31
Copy link
Contributor

@akshayjshah akshayjshah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits, otherwise lgtm.

withJSONLogger(t, DebugLevel, opts(AddCallerSkip(1)), func(logger Logger, buf *testBuffer) {
logger.Info("Callers.")
assert.Regexp(t,
`"caller":"[^"]+/common_test.go:\d+","msg":"Callers\."`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also test that

if ce := logger.Check(InfoLevel, "Callers"); ce != nil {
  ce.Write()
}

gets the correct caller. This should be the long-awaited fix for #40.

return optionFunc(func(log *logger) {
log.addCaller = true
if n := skip + _defaultCallerSkip; n > log.callerSkip {
log.callerSkip = n
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think of these as orthogonal options: AddCaller is for users to indicate that they'd like to include caller names, and AddCallerSkip is for extension authors to make sure that the logged caller is accurate. I imagine wrapper authors, including the proposed sugared logger, taking a slice of options from the user and adding on AddCallerSkip(n) (to account for the frames occupied by their wrapper) before passing the result on to the inner logger constructor (which might itself be a wrapper).

Given that, I think that this option should just do log.callerSkip += skip.

@@ -144,7 +144,7 @@ func (log *logger) Check(lvl Level, msg string) *CheckedEntry {
ce.ErrorOutput = log.errorOutput

if log.addCaller {
ce.Entry.Caller = MakeEntryCaller(runtime.Caller(log.callerSkip))
ce.Entry.Caller = MakeEntryCaller(runtime.Caller(log.callerSkip + 2))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we preserve the defaultCallerSkip constant instead of putting a literal 2 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I'd argue for leaving it in place:

  • it truly is path dependent on how logger calls check
  • originally I started out with it as a third arg to the new check method...
  • ...which was either 2 or 3 depending on the entry path call site to check
  • however, after I modified all the named entries (Debug, Info, Warn, etc) to call check instead of Log, it turned out that all the sites were passing the same 2 skip arg

I'd be happy to re-introduce a constant, but:

  • it doesn't deserve to be package level any more
  • rather it would be at the top of check, and called something like skipOffset

I'll do that since it'll dovetail with any future needs if we ever need to re-promote it to a third check arg.

if ce := log.check(FatalLevel, msg); ce != nil {
ce.Write(fields...)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yuck. I suppose this is a necessary evil to keep the skip level consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is, and this actually was an untested bug in prior (and I'm still lacking a case for it actually):

  • Log is an entry point...
  • ...but so are Debug, Info, etc.

So all the named entries need +1 more offset than Log ... but we have no support for dynamically modifying skip (and we're trying very hard to avoid adding such).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm the point about Log is an entry point: it's not, we dropped it in #201, but the implementation lived on here; will drop.

testCase.name)
}

assert.Empty(t, errSink.String(), "Expected error sink to be empty.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we structure this so that each test case includes the logger and the expected message, and in the test loop we

tt.logger.Info("foo")
tt.logger.Check(InfoLevel, "foo").Write()

and then assert that we got the expected message twice? Seems like that would more clearly associate the loggers and the expectations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, this code is a bit more repetitive than prior because it's a "contraction" back away from a moment of too much indirection; I'll start layering back some ;-)

// AddCallerSkip configures the Logger to annotate each message with the filename
// and line number of a caller outside of zap; which caller in the stack is
// controlled by the skip argument. If more than one AddCallerSkip option is
// provided, then the maximum skip amount is used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "the sum of the skip amounts is used." Also, to me the comment suggests that this option also enables caller annotations - may want to clarify.

@jcorbin jcorbin merged commit 07302c1 into dev Jan 10, 2017
@jcorbin jcorbin deleted the jcorbin.callerskip branch January 10, 2017 01:11
akshayjshah pushed a commit that referenced this pull request Feb 15, 2017
* Add AddCallerSkip option
* Improve and expand caller skip test
* Fix logger Check v Level-log skipping
* logger: drop old Log method; not actually part of the interface since #201
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.

3 participants