Address PR comments

This commit is contained in:
Ben Brooks
2017-02-15 13:08:26 +00:00
parent b545aee819
commit e98cd92ccf
2 changed files with 26 additions and 21 deletions
+15 -13
View File
@@ -7,7 +7,6 @@ import (
"strings"
"sync"
"time"
"unicode/utf8"
)
const (
@@ -53,8 +52,9 @@ type TextFormatter struct {
// QuoteEmptyFields will wrap empty fields in quotes if true
QuoteEmptyFields bool
// QuoteRune can be set to override the default quote style
QuoteRune rune
// QuoteCharacter can be set to the override the default quoting character "
// with something else. For example: ', or `.
QuoteCharacter string
// Whether the logger's out is to a terminal
isTerminal bool
@@ -62,6 +62,15 @@ type TextFormatter struct {
sync.Once
}
func (f *TextFormatter) init(entry *Entry) {
if len(f.QuoteCharacter) == 0 {
f.QuoteCharacter = "\""
}
if entry.Logger != nil {
f.isTerminal = IsTerminal(entry.Logger.Out)
}
}
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
var b *bytes.Buffer
keys := make([]string, 0, len(entry.Data))
@@ -80,14 +89,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
prefixFieldClashes(entry.Data)
f.Do(func() {
if f.QuoteRune == 0 || !utf8.ValidRune(f.QuoteRune) {
f.QuoteRune = '"'
}
if entry.Logger != nil {
f.isTerminal = IsTerminal(entry.Logger.Out)
}
})
f.Do(func() { f.init(entry) })
isColored := (f.ForceColors || f.isTerminal) && !f.DisableColors
@@ -172,14 +174,14 @@ func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) {
if !f.needsQuoting(value) {
b.WriteString(value)
} else {
fmt.Fprintf(b, "%c%v%c", f.QuoteRune, value, f.QuoteRune)
fmt.Fprintf(b, "%s%v%s", f.QuoteCharacter, value, f.QuoteCharacter)
}
case error:
errmsg := value.Error()
if !f.needsQuoting(errmsg) {
b.WriteString(errmsg)
} else {
fmt.Fprintf(b, "%c%v%c", f.QuoteRune, errmsg, f.QuoteRune)
fmt.Fprintf(b, "%s%v%s", f.QuoteCharacter, errmsg, f.QuoteCharacter)
}
default:
fmt.Fprint(b, value)