From e8a8508f09b9d56fe915b9d928a8f2306e4fab78 Mon Sep 17 00:00:00 2001 From: su21 Date: Wed, 25 Jul 2018 17:48:22 +0800 Subject: [PATCH] fix caller file and line number in context hook (#89) * fix caller file and line number report in context hook * update comment * update comment --- context.go | 6 +++--- event.go | 29 +++++++++++++++++------------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/context.go b/context.go index 9800eb6d..c277fd28 100644 --- a/context.go +++ b/context.go @@ -108,7 +108,7 @@ func (c Context) AnErr(key string, err error) Context { case nil: return c case LogObjectMarshaler: - return c.Object(key,m) + return c.Object(key, m) case error: return c.Str(key, m.Error()) case string: @@ -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). + e.caller(CallerSkipFrameCount + 3) } var ch = callerHook{} diff --git a/event.go b/event.go index 8a7abf0d..8468e417 100644 --- a/event.go +++ b/event.go @@ -19,7 +19,7 @@ var eventPool = &sync.Pool{ } // ErrorMarshalFunc allows customization of global error marshaling -var ErrorMarshalFunc = func (err error) interface{} { +var ErrorMarshalFunc = func(err error) interface{} { return err } @@ -83,6 +83,21 @@ func (e *Event) Msg(msg string) { if e == nil { return } + e.msg(msg) +} + +// Msgf sends the event with formated msg added as the message field if not empty. +// +// NOTICE: once this methid is called, the *Event should be disposed. +// Calling Msg twice can have unexpected result. +func (e *Event) Msgf(format string, v ...interface{}) { + if e == nil { + return + } + e.msg(fmt.Sprintf(format, v...)) +} + +func (e *Event) msg(msg string) { if len(e.ch) > 0 { e.ch[0].Run(e, e.level, msg) if len(e.ch) > 1 { @@ -110,17 +125,6 @@ func (e *Event) Msg(msg string) { } } -// Msgf sends the event with formated msg added as the message field if not empty. -// -// NOTICE: once this methid is called, the *Event should be disposed. -// Calling Msg twice can have unexpected result. -func (e *Event) Msgf(format string, v ...interface{}) { - if e == nil { - return - } - e.Msg(fmt.Sprintf(format, v...)) -} - // Fields is a helper function to use a map to set fields using type assertion. func (e *Event) Fields(fields map[string]interface{}) *Event { if e == nil { @@ -261,6 +265,7 @@ func (e *Event) AnErr(key string, err error) *Event { return e.Interface(key, m) } } + // Errs adds the field key with errs as an array of serialized errors to the // *Event context. func (e *Event) Errs(key string, errs []error) *Event {