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 hooks to logger #25

Merged
merged 3 commits into from
Apr 8, 2016
Merged

Add hooks to logger #25

merged 3 commits into from
Apr 8, 2016

Conversation

akshayjshah
Copy link
Contributor

Extend the logger to support a variable number of pre-log hooks. The
hooks can alter the in-flight message and/or add to the logging context.

For this first revision, I've included hooks to (a) annotate each log
message with the logging call site, and (b) include stack traces for all
log messages above a given level.

@akshayjshah akshayjshah force-pushed the ajs-thunks branch 2 times, most recently from 0b08b58 to cfef0be Compare April 7, 2016 22:14

func TestHookAddCaller(t *testing.T) {
buf := &bytes.Buffer{}
logger := NewJSON(All, Output(buf), ErrorOutput(ioutil.Discard), Hooks(AddCaller()))
Copy link
Collaborator

Choose a reason for hiding this comment

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

here and below, suggest skipping the ErrorOutput option since the tests shouldn't produce errors. If they do, they'll go to stderr and we'll be more likely to notice them.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could our default hooks just implement Option so you can pass them directly without having to rewrap them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea. Done!

Extend the logger to support a variable number of pre-log hooks. The
hooks can alter the in-flight message and/or add to the logging context.

For this first revision, I've included hooks to (a) annotate each log
message with the logging call site, and (b) include stack traces for all
log messages above a given level.
logger := NewJSON(All, Output(buf), AddCaller())
logger.Info("Callers.")

re := regexp.MustCompile(`hook_test.go:[\d]+: Callers\.`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: can we ensure that only the base path is included, by making this look for "msg":"hook_test.go:[...]

@prashantv
Copy link
Collaborator

lgtm

@akshayjshah akshayjshah merged commit 298d211 into master Apr 8, 2016
@akshayjshah akshayjshah deleted the ajs-thunks branch April 8, 2016 23:38
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.

2 participants