fix caller file and line number in context hook (#89)

* fix caller file and line number report in context hook

* update comment

* update comment
This commit is contained in:
su21 2018-07-25 17:48:22 +08:00 committed by Olivier Poitrey
parent 372015deb4
commit e8a8508f09
2 changed files with 20 additions and 15 deletions

View File

@ -108,7 +108,7 @@ func (c Context) AnErr(key string, err error) Context {
case nil: case nil:
return c return c
case LogObjectMarshaler: case LogObjectMarshaler:
return c.Object(key,m) return c.Object(key, m)
case error: case error:
return c.Str(key, m.Error()) return c.Str(key, m.Error())
case string: case string:
@ -350,8 +350,8 @@ func (c Context) Interface(key string, i interface{}) Context {
type callerHook struct{} type callerHook struct{}
func (ch callerHook) Run(e *Event, level Level, msg string) { func (ch callerHook) Run(e *Event, level Level, msg string) {
//Two extra frames to skip (added by hook infra). // Three extra frames to skip (added by hook infra).
e.caller(CallerSkipFrameCount+2) e.caller(CallerSkipFrameCount + 3)
} }
var ch = callerHook{} var ch = callerHook{}

View File

@ -19,7 +19,7 @@ var eventPool = &sync.Pool{
} }
// ErrorMarshalFunc allows customization of global error marshaling // ErrorMarshalFunc allows customization of global error marshaling
var ErrorMarshalFunc = func (err error) interface{} { var ErrorMarshalFunc = func(err error) interface{} {
return err return err
} }
@ -83,6 +83,21 @@ func (e *Event) Msg(msg string) {
if e == nil { if e == nil {
return 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 { if len(e.ch) > 0 {
e.ch[0].Run(e, e.level, msg) e.ch[0].Run(e, e.level, msg)
if len(e.ch) > 1 { 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. // Fields is a helper function to use a map to set fields using type assertion.
func (e *Event) Fields(fields map[string]interface{}) *Event { func (e *Event) Fields(fields map[string]interface{}) *Event {
if e == nil { if e == nil {
@ -261,6 +265,7 @@ func (e *Event) AnErr(key string, err error) *Event {
return e.Interface(key, m) return e.Interface(key, m)
} }
} }
// Errs adds the field key with errs as an array of serialized errors to the // Errs adds the field key with errs as an array of serialized errors to the
// *Event context. // *Event context.
func (e *Event) Errs(key string, errs []error) *Event { func (e *Event) Errs(key string, errs []error) *Event {