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 ""
}
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) {

View File

@ -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)

View File

@ -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"])
}

View File

@ -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)
}