-
Notifications
You must be signed in to change notification settings - Fork 578
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
fix caller file and line number in context hook #89
Conversation
@@ -83,6 +83,21 @@ func (e *Event) Msg(msg string) { | |||
if e == nil { | |||
return | |||
} | |||
e.msg(msg) |
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.
What's the goal of having both Msg
and Msgf
paying to cost of an extra function call here? It's also not relevant with the subject of the PR.
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.
Context.Caller()
add a custom hook which is called by Event.Msg
.
If people use both Context.Caller
and Event.Msgf
, they will see wrong caller.
Because Msgf
call Msg
:
Msgf-> Msg -> HookBlabla -> runtime.Caller
you can reproduce in TestWith
with this patch:
index e013606..52c2294 100644
--- a/log_test.go
+++ b/log_test.go
@@ -101,7 +101,7 @@ func TestWith(t *testing.T) {
_, file, line, _ := runtime.Caller(0)
caller := fmt.Sprintf("%s:%d", file, line+3)
log := ctx.Caller().Logger()
- log.Log().Msg("")
+ log.Log().Msgf("")
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":"foo","bytes":"bar","hex":"12ef","json":{"some":"json"},"error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11.101,"float64":12.30303,"time":"0001-01-01T00:00:00Z","caller":"`+caller+`"}`+"\n"; got != want {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
}
--- FAIL: TestWith (0.00s)
log_test.go:106: invalid log output:
got: {"string":"foo","bytes":"bar","hex":"12ef","json":{"some":"json"},"error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11.101,"float64":12.30303,"time":"0001-01-01T00:00:00Z","caller":"/home/supei/gohome/src/github.com/rs/zerolog/event.go:121"}
want: {"string":"foo","bytes":"bar","hex":"12ef","json":{"some":"json"},"error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11.101,"float64":12.30303,"time":"0001-01-01T00:00:00Z","caller":"/home/supei/gohome/src/github.com/rs/zerolog/log_test.go:104"}FAIL
exit status 1
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.
Oh, ok got it. Thx for the clarification.
context.go
Outdated
@@ -350,8 +350,8 @@ func (c Context) Interface(key string, i interface{}) Context { | |||
type callerHook struct{} | |||
|
|||
func (ch callerHook) Run(e *Event, level Level, msg string) { | |||
//Two extra frames to skip (added by hook infra). | |||
e.caller(CallerSkipFrameCount+2) | |||
//three extra frames to skip (added by hook infra). |
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.
Please add a cap to the T and a space after the //
.
fix #88
@rs ptal