From 61891c49c9aedc44ae975a1dd16e4e5b2a47c7c8 Mon Sep 17 00:00:00 2001 From: wk30 Date: Wed, 16 Sep 2020 12:27:46 +0800 Subject: [PATCH] support caller manually --- entry.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/entry.go b/entry.go index 5a5cbfe..71b7120 100644 --- a/entry.go +++ b/entry.go @@ -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)