text_formatter: use %v instead

This commit is contained in:
Simon Eskildsen 2014-03-14 13:15:01 -04:00
parent f84cc365b1
commit 52eb1c0f57
1 changed files with 19 additions and 22 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"os" "os"
"sort" "sort"
"strconv"
"strings" "strings"
"github.com/burke/ttyutils" "github.com/burke/ttyutils"
@ -62,8 +61,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
for key, value := range entry.Data { for key, value := range entry.Data {
if key != "time" && key != "level" && key != "msg" { if key != "time" && key != "level" && key != "msg" {
serialized = append(serialized, []byte(fmt.Sprintf("%s=%s ", serialized = f.AppendKeyValue(serialized, key, value)
f.ToString(key, false), f.ToString(value, true)))...)
} }
} }
} }
@ -71,24 +69,23 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
return append(serialized, '\n'), nil return append(serialized, '\n'), nil
} }
func (f *TextFormatter) AppendKeyValue(serialized []byte, key, value string) []byte { func (f *TextFormatter) AppendKeyValue(serialized []byte, key, value interface{}) []byte {
return append(serialized, []byte(fmt.Sprintf("%s=%s ", return append(serialized, []byte(fmt.Sprintf("%v=%v ", key, value))...)
f.ToString(key, false), f.ToString(value, true)))...)
} }
func (f *TextFormatter) ToString(value interface{}, escapeStrings bool) string { // func (f *TextFormatter) ToString(value interface{}, escapeStrings bool) string {
switch value.(type) { // switch value.(type) {
default: // default:
if escapeStrings { // if escapeStrings {
return fmt.Sprintf("'%s'", value) // return fmt.Sprintf("'%s'", value)
} else { // } else {
return fmt.Sprintf("%s", value) // return fmt.Sprintf("%s", value)
} // }
case int: // case int:
return fmt.Sprintf("%s", strconv.Itoa(value.(int))) // return fmt.Sprintf("%s", strconv.Itoa(value.(int)))
case uint64: // case uint64:
return fmt.Sprintf("%s", strconv.FormatUint(value.(uint64), 10)) // return fmt.Sprintf("%s", strconv.FormatUint(value.(uint64), 10))
case bool: // case bool:
return fmt.Sprintf("%s", strconv.FormatBool(value.(bool))) // return fmt.Sprintf("%s", strconv.FormatBool(value.(bool)))
} // }
} // }