Skip to content

Commit

Permalink
text_formatter: fix race
Browse files Browse the repository at this point in the history
  • Loading branch information
sirupsen committed Feb 7, 2017
1 parent 1726e17 commit 11fbf0f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/Sirupsen/logrus"
"os"
)

var log = logrus.New()
Expand Down
3 changes: 3 additions & 0 deletions formatter_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ func BenchmarkLargeJSONFormatter(b *testing.B) {
}

func doBenchmark(b *testing.B, formatter Formatter, fields Fields) {
logger := New()

entry := &Entry{
Time: time.Time{},
Level: InfoLevel,
Message: "message",
Data: fields,
Logger: logger,
}
var d []byte
var err error
Expand Down
14 changes: 8 additions & 6 deletions text_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"sort"
"strings"
"sync"
"time"
)

Expand Down Expand Up @@ -49,8 +50,8 @@ type TextFormatter struct {
DisableSorting bool

// Whether the logger's out is to a terminal
isTerminal bool
terminalDetermined bool
isTerminal bool
terminalOnce sync.Once
}

func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
Expand All @@ -71,10 +72,11 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {

prefixFieldClashes(entry.Data)

if !f.terminalDetermined {
f.isTerminal = IsTerminal(entry.Logger.Out)
f.terminalDetermined = true
}
f.terminalOnce.Do(func() {
if entry.Logger != nil {
f.isTerminal = IsTerminal(entry.Logger.Out)
}
})

isColored := (f.ForceColors || f.isTerminal) && !f.DisableColors

Expand Down

0 comments on commit 11fbf0f

Please sign in to comment.