-
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
Add hooks to logger #25
Conversation
0b08b58
to
cfef0be
Compare
|
||
func TestHookAddCaller(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
logger := NewJSON(All, Output(buf), ErrorOutput(ioutil.Discard), Hooks(AddCaller())) |
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.
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.
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.
Could our default hooks just implement Option
so you can pass them directly without having to rewrap them?
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.
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\.`) |
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.
nit: can we ensure that only the base path is included, by making this look for "msg":"hook_test.go:[...]
lgtm |
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.