Skip to content

Commit

Permalink
Merge pull request #854 from sirupsen/fix_panic_text_formatter
Browse files Browse the repository at this point in the history
fix panic in text formatter
  • Loading branch information
dgsb authored Oct 31, 2018
2 parents 566a5f6 + d10c2f9 commit fd23083
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
12 changes: 12 additions & 0 deletions logrus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,3 +702,15 @@ func TestLogLevelEnabled(t *testing.T) {
assert.Equal(t, true, log.IsLevelEnabled(DebugLevel))
assert.Equal(t, true, log.IsLevelEnabled(TraceLevel))
}

func TestReportCallerOnTextFormatter(t *testing.T) {
l := New()

l.Formatter.(*TextFormatter).ForceColors = true
l.Formatter.(*TextFormatter).DisableColors = false
l.WithFields(Fields{"func": "func", "file": "file"}).Info("test")

l.Formatter.(*TextFormatter).ForceColors = false
l.Formatter.(*TextFormatter).DisableColors = true
l.WithFields(Fields{"func": "func", "file": "file"}).Info("test")
}
14 changes: 7 additions & 7 deletions text_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,18 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
} else {
for _, key := range fixedKeys {
var value interface{}
switch key {
case f.FieldMap.resolve(FieldKeyTime):
switch {
case key == f.FieldMap.resolve(FieldKeyTime):
value = entry.Time.Format(timestampFormat)
case f.FieldMap.resolve(FieldKeyLevel):
case key == f.FieldMap.resolve(FieldKeyLevel):
value = entry.Level.String()
case f.FieldMap.resolve(FieldKeyMsg):
case key == f.FieldMap.resolve(FieldKeyMsg):
value = entry.Message
case f.FieldMap.resolve(FieldKeyLogrusError):
case key == f.FieldMap.resolve(FieldKeyLogrusError):
value = entry.err
case f.FieldMap.resolve(FieldKeyFunc):
case key == f.FieldMap.resolve(FieldKeyFunc) && entry.HasCaller():
value = entry.Caller.Function
case f.FieldMap.resolve(FieldKeyFile):
case key == f.FieldMap.resolve(FieldKeyFile) && entry.HasCaller():
value = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
default:
value = entry.Data[key]
Expand Down

0 comments on commit fd23083

Please sign in to comment.