text-formatter: assume that IsTerminal doesn't change during execution

This commit is contained in:
Antoine Grondin 2014-09-16 17:19:03 -04:00
parent 8a758beb29
commit d35b1c983e
2 changed files with 10 additions and 5 deletions

View File

@ -28,8 +28,6 @@ type Entry struct {
Message string
}
var baseTimestamp time.Time
func NewEntry(logger *Logger) *Entry {
return &Entry{
Logger: logger,

View File

@ -16,8 +16,14 @@ const (
blue = 34
)
var (
baseTimestamp time.Time
isTerminal bool
)
func init() {
baseTimestamp = time.Now()
isTerminal = IsTerminal()
}
func miniTS() int {
@ -31,11 +37,12 @@ type TextFormatter struct {
}
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
b := &bytes.Buffer{}
prefixFieldClashes(entry)
if (f.ForceColors || IsTerminal()) && !f.DisableColors {
if (f.ForceColors || isTerminal) && !f.DisableColors {
levelText := strings.ToUpper(entry.Data["level"].(string))[0:4]
levelColor := blue
@ -50,8 +57,8 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d] %-44s ", levelColor, levelText, miniTS(), entry.Data["msg"])
keys := make([]string, 0)
for k, _ := range entry.Data {
var keys []string
for k := range entry.Data {
if k != "level" && k != "time" && k != "msg" {
keys = append(keys, k)
}