diff --git a/entry.go b/entry.go index 9e86687..d3b1aa9 100644 --- a/entry.go +++ b/entry.go @@ -126,6 +126,12 @@ func getCaller() (method string) { return "" } +func (entry Entry) HasCaller() (has bool) { + return entry.Logger != nil && + entry.Logger.ReportCaller && + entry.Caller != "" +} + // This function is not declared with a pointer value because otherwise // race conditions will occur when using multiple goroutines func (entry Entry) log(level Level, msg string) { diff --git a/json_formatter.go b/json_formatter.go index 46498da..efb9a9a 100644 --- a/json_formatter.go +++ b/json_formatter.go @@ -53,12 +53,7 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) { } } - reportCaller := false - if entry.Logger != nil { - reportCaller = entry.Logger.ReportCaller - } - - prefixFieldClashes(data, reportCaller) + prefixFieldClashes(data, entry.HasCaller()) timestampFormat := f.TimestampFormat if timestampFormat == "" { @@ -68,7 +63,7 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) { data[f.FieldMap.resolve(FieldKeyTime)] = entry.Time.Format(timestampFormat) data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String() - if reportCaller { + if entry.HasCaller() { data[f.FieldMap.resolve(FieldKeyCaller)] = entry.Caller } serialized, err := json.Marshal(data) diff --git a/json_formatter_test.go b/json_formatter_test.go index 8235224..05cd51e 100644 --- a/json_formatter_test.go +++ b/json_formatter_test.go @@ -193,9 +193,9 @@ func TestFieldDoesNotClashWithCaller(t *testing.T) { func TestFieldClashWithCaller(t *testing.T) { SetReportCaller(true) formatter := &JSONFormatter{} - std.ReportCaller = true - - b, err := formatter.Format(WithField("method", "howdy pardner")) + e := WithField("method", "howdy pardner") + e.Caller = "somefunc" + b, err := formatter.Format(e) if err != nil { t.Fatal("Unable to format entry: ", err) } @@ -211,7 +211,7 @@ func TestFieldClashWithCaller(t *testing.T) { entry["fields.method"]) } - if entry["method"] != "" { // since we didn't actually log, it's set to the empty string + if entry["method"] != "somefunc" { t.Fatalf("method not set as expected when ReportCaller=true (got '%s')", entry["method"]) } diff --git a/text_formatter.go b/text_formatter.go index 91715f8..b0fa789 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -72,12 +72,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { b = &bytes.Buffer{} } - reportCaller := false - if entry.Logger != nil { - reportCaller = entry.Logger.ReportCaller - } - - prefixFieldClashes(entry.Data, reportCaller) + prefixFieldClashes(entry.Data, entry.HasCaller()) isColorTerminal := isTerminal && (runtime.GOOS != "windows") isColored := (f.ForceColors || isColorTerminal) && !f.DisableColors @@ -93,7 +88,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { f.appendKeyValue(b, "time", entry.Time.Format(timestampFormat)) } f.appendKeyValue(b, "level", entry.Level.String()) - if reportCaller { + if entry.HasCaller() { f.appendKeyValue(b, "method", entry.Caller) } if entry.Message != "" { @@ -125,12 +120,7 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin caller := "" - reportCaller := false - if entry.Logger != nil { - reportCaller = entry.Logger.ReportCaller - } - - if reportCaller { + if entry.HasCaller() { caller = fmt.Sprintf(" %s()", entry.Caller) }