mirror of https://github.com/sirupsen/logrus.git
text_formatter: change to simpler k-v when no tty
This commit is contained in:
parent
ddcbea3dba
commit
1000975e52
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/burke/ttyutils"
|
"github.com/burke/ttyutils"
|
||||||
|
@ -23,37 +24,71 @@ type TextFormatter struct {
|
||||||
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
|
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
var serialized []byte
|
var serialized []byte
|
||||||
|
|
||||||
levelText := strings.ToUpper(entry.Data["level"].(string))[0:4]
|
|
||||||
levelColor := blue
|
|
||||||
|
|
||||||
if entry.Data["level"] == "warning" {
|
|
||||||
levelColor = yellow
|
|
||||||
} else if entry.Data["level"] == "fatal" ||
|
|
||||||
entry.Data["level"] == "panic" {
|
|
||||||
levelColor = red
|
|
||||||
}
|
|
||||||
|
|
||||||
if ttyutils.IsTerminal(os.Stdout.Fd()) {
|
if ttyutils.IsTerminal(os.Stdout.Fd()) {
|
||||||
serialized = append(serialized, []byte(fmt.Sprintf("\x1b[%dm%s\x1b[0m[%04d] %-45s ", levelColor, levelText, miniTS(), entry.Data["msg"]))...)
|
levelText := strings.ToUpper(entry.Data["level"].(string))[0:4]
|
||||||
}
|
|
||||||
|
|
||||||
keys := make([]string, 0)
|
levelColor := blue
|
||||||
for k, _ := range entry.Data {
|
|
||||||
if k != "level" && k != "time" && k != "msg" {
|
if entry.Data["level"] == "warning" {
|
||||||
keys = append(keys, k)
|
levelColor = yellow
|
||||||
|
} else if entry.Data["level"] == "fatal" ||
|
||||||
|
entry.Data["level"] == "panic" {
|
||||||
|
levelColor = red
|
||||||
}
|
}
|
||||||
}
|
|
||||||
sort.Strings(keys)
|
serialized = append(serialized, []byte(fmt.Sprintf("\x1b[%dm%s\x1b[0m[%04d] %-45s ", levelColor, levelText, miniTS(), entry.Data["msg"]))...)
|
||||||
first := true
|
|
||||||
for _, k := range keys {
|
keys := make([]string, 0)
|
||||||
v := entry.Data[k]
|
for k, _ := range entry.Data {
|
||||||
if first {
|
if k != "level" && k != "time" && k != "msg" {
|
||||||
first = false
|
keys = append(keys, k)
|
||||||
} else {
|
}
|
||||||
serialized = append(serialized, ' ')
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
first := true
|
||||||
|
for _, k := range keys {
|
||||||
|
v := entry.Data[k]
|
||||||
|
if first {
|
||||||
|
first = false
|
||||||
|
} else {
|
||||||
|
serialized = append(serialized, ' ')
|
||||||
|
}
|
||||||
|
serialized = append(serialized, []byte(fmt.Sprintf("\x1b[%dm%s\x1b[0m=%v", levelColor, k, v))...)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
serialized = f.AppendKeyValue(serialized, "time", entry.Data["time"].(string))
|
||||||
|
serialized = f.AppendKeyValue(serialized, "level", entry.Data["level"].(string))
|
||||||
|
serialized = f.AppendKeyValue(serialized, "msg", entry.Data["msg"].(string))
|
||||||
|
|
||||||
|
for key, value := range entry.Data {
|
||||||
|
if key != "time" && key != "level" && key != "msg" {
|
||||||
|
serialized = append(serialized, []byte(fmt.Sprintf("%s=%s ",
|
||||||
|
f.ToString(key, false), f.ToString(value, true)))...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
serialized = append(serialized, []byte(fmt.Sprintf("\x1b[%dm%s\x1b[0m=%v", levelColor, k, v))...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return append(serialized, '\n'), nil
|
return append(serialized, '\n'), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *TextFormatter) AppendKeyValue(serialized []byte, key, value string) []byte {
|
||||||
|
return append(serialized, []byte(fmt.Sprintf("%s=%s ",
|
||||||
|
f.ToString(key, false), f.ToString(value, true)))...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *TextFormatter) ToString(value interface{}, escapeStrings bool) string {
|
||||||
|
switch value.(type) {
|
||||||
|
default:
|
||||||
|
if escapeStrings {
|
||||||
|
return fmt.Sprintf("'%s'", value)
|
||||||
|
} else {
|
||||||
|
return fmt.Sprintf("%s", value)
|
||||||
|
}
|
||||||
|
case int:
|
||||||
|
return fmt.Sprintf("%s", strconv.Itoa(value.(int)))
|
||||||
|
case uint64:
|
||||||
|
return fmt.Sprintf("%s", strconv.FormatUint(value.(uint64), 10))
|
||||||
|
case bool:
|
||||||
|
return fmt.Sprintf("%s", strconv.FormatBool(value.(bool)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue