From 570db1b0b9f51afe7da19539a75fd2c5232edf46 Mon Sep 17 00:00:00 2001 From: Peng Zhai Date: Thu, 16 Jul 2015 21:02:46 -0400 Subject: [PATCH 1/2] Fix data race issue in TextFormatter. Fix for https://github.com/Sirupsen/logrus/issues/217. --- text_formatter.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/text_formatter.go b/text_formatter.go index e25f86c..5f9ab46 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -73,14 +73,15 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { isColorTerminal := isTerminal && (runtime.GOOS != "windows") isColored := (f.ForceColors || isColorTerminal) && !f.DisableColors - if f.TimestampFormat == "" { - f.TimestampFormat = DefaultTimestampFormat + timestampFormat := f.TimestampFormat + if timestampFormat == "" { + timestampFormat = DefaultTimestampFormat } if isColored { f.printColored(b, entry, keys) } else { if !f.DisableTimestamp { - f.appendKeyValue(b, "time", entry.Time.Format(f.TimestampFormat)) + f.appendKeyValue(b, "time", entry.Time.Format(timestampFormat)) } f.appendKeyValue(b, "level", entry.Level.String()) f.appendKeyValue(b, "msg", entry.Message) From a8127fd48517602b678dbdbef16351b4cf9708b9 Mon Sep 17 00:00:00 2001 From: Peng Zhai Date: Mon, 20 Jul 2015 10:43:12 -0700 Subject: [PATCH 2/2] Change printColored to use timestampFormat. --- text_formatter.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/text_formatter.go b/text_formatter.go index 5f9ab46..17cc298 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -78,7 +78,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { timestampFormat = DefaultTimestampFormat } if isColored { - f.printColored(b, entry, keys) + f.printColored(b, entry, keys, timestampFormat) } else { if !f.DisableTimestamp { f.appendKeyValue(b, "time", entry.Time.Format(timestampFormat)) @@ -94,7 +94,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { return b.Bytes(), nil } -func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string) { +func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, timestampFormat string) { var levelColor int switch entry.Level { case DebugLevel: @@ -112,7 +112,7 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin if !f.FullTimestamp { fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d] %-44s ", levelColor, levelText, miniTS(), entry.Message) } else { - fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %-44s ", levelColor, levelText, entry.Time.Format(f.TimestampFormat), entry.Message) + fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), entry.Message) } for _, k := range keys { v := entry.Data[k]