support caller manually

This commit is contained in:
wk30 2020-09-16 12:27:46 +08:00
parent 6699a89a23
commit 61891c49c9
1 changed files with 13 additions and 0 deletions

View File

@ -94,6 +94,19 @@ func (entry *Entry) String() (string, error) {
return str, nil
}
// Add a caller field to the Entry.
func (entry *Entry) WithCaller() *Entry {
frame := getCaller()
if frame != nil {
return entry.WithFields(Fields{
FieldKeyFile: fmt.Sprintf("%s:%d", frame.File, frame.Line),
FieldKeyFunc: frame.Function,
})
}
return entry
}
// Add an error as single field (using the key defined in ErrorKey) to the Entry.
func (entry *Entry) WithError(err error) *Entry {
return entry.WithField(ErrorKey, err)