simplify hasCaller check

This commit is contained in:
Dave Clendenan 2016-11-30 15:15:38 -08:00
parent a5c845c224
commit 65f3af38f7
4 changed files with 15 additions and 24 deletions

View File

@ -126,6 +126,12 @@ func getCaller() (method string) {
return "" 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 // This function is not declared with a pointer value because otherwise
// race conditions will occur when using multiple goroutines // race conditions will occur when using multiple goroutines
func (entry Entry) log(level Level, msg string) { func (entry Entry) log(level Level, msg string) {

View File

@ -53,12 +53,7 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
} }
} }
reportCaller := false prefixFieldClashes(data, entry.HasCaller())
if entry.Logger != nil {
reportCaller = entry.Logger.ReportCaller
}
prefixFieldClashes(data, reportCaller)
timestampFormat := f.TimestampFormat timestampFormat := f.TimestampFormat
if 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(FieldKeyTime)] = entry.Time.Format(timestampFormat)
data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message
data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String() data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String()
if reportCaller { if entry.HasCaller() {
data[f.FieldMap.resolve(FieldKeyCaller)] = entry.Caller data[f.FieldMap.resolve(FieldKeyCaller)] = entry.Caller
} }
serialized, err := json.Marshal(data) serialized, err := json.Marshal(data)

View File

@ -193,9 +193,9 @@ func TestFieldDoesNotClashWithCaller(t *testing.T) {
func TestFieldClashWithCaller(t *testing.T) { func TestFieldClashWithCaller(t *testing.T) {
SetReportCaller(true) SetReportCaller(true)
formatter := &JSONFormatter{} formatter := &JSONFormatter{}
std.ReportCaller = true e := WithField("method", "howdy pardner")
e.Caller = "somefunc"
b, err := formatter.Format(WithField("method", "howdy pardner")) b, err := formatter.Format(e)
if err != nil { if err != nil {
t.Fatal("Unable to format entry: ", err) t.Fatal("Unable to format entry: ", err)
} }
@ -211,7 +211,7 @@ func TestFieldClashWithCaller(t *testing.T) {
entry["fields.method"]) 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')", t.Fatalf("method not set as expected when ReportCaller=true (got '%s')",
entry["method"]) entry["method"])
} }

View File

@ -72,12 +72,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
b = &bytes.Buffer{} b = &bytes.Buffer{}
} }
reportCaller := false prefixFieldClashes(entry.Data, entry.HasCaller())
if entry.Logger != nil {
reportCaller = entry.Logger.ReportCaller
}
prefixFieldClashes(entry.Data, reportCaller)
isColorTerminal := isTerminal && (runtime.GOOS != "windows") isColorTerminal := isTerminal && (runtime.GOOS != "windows")
isColored := (f.ForceColors || isColorTerminal) && !f.DisableColors 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, "time", entry.Time.Format(timestampFormat))
} }
f.appendKeyValue(b, "level", entry.Level.String()) f.appendKeyValue(b, "level", entry.Level.String())
if reportCaller { if entry.HasCaller() {
f.appendKeyValue(b, "method", entry.Caller) f.appendKeyValue(b, "method", entry.Caller)
} }
if entry.Message != "" { if entry.Message != "" {
@ -125,12 +120,7 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin
caller := "" caller := ""
reportCaller := false if entry.HasCaller() {
if entry.Logger != nil {
reportCaller = entry.Logger.ReportCaller
}
if reportCaller {
caller = fmt.Sprintf(" %s()", entry.Caller) caller = fmt.Sprintf(" %s()", entry.Caller)
} }