Skip to content

Commit

Permalink
fix caller file and line number in context hook (#89)
Browse files Browse the repository at this point in the history
* fix caller file and line number report in context hook

* update comment

* update comment
  • Loading branch information
patricksuo authored and rs committed Jul 25, 2018
1 parent 372015d commit e8a8508
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
6 changes: 3 additions & 3 deletions context.go
Expand Up @@ -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:
Expand Down Expand Up @@ -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{}
Expand Down
29 changes: 17 additions & 12 deletions event.go
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e8a8508

Please sign in to comment.