2014-03-11 03:22:08 +04:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
2014-05-24 18:46:03 +04:00
|
|
|
"bytes"
|
2014-03-11 03:22:08 +04:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2014-05-04 04:43:55 +04:00
|
|
|
"time"
|
2014-03-11 03:22:08 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
nocolor = 0
|
|
|
|
red = 31
|
|
|
|
green = 32
|
|
|
|
yellow = 33
|
|
|
|
blue = 34
|
|
|
|
)
|
|
|
|
|
2014-05-04 04:43:55 +04:00
|
|
|
func init() {
|
|
|
|
baseTimestamp = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
func miniTS() int {
|
|
|
|
return int(time.Since(baseTimestamp) / time.Second)
|
|
|
|
}
|
|
|
|
|
2014-03-11 03:22:08 +04:00
|
|
|
type TextFormatter struct {
|
2014-03-16 07:36:51 +04:00
|
|
|
// Set to true to bypass checking for a TTY before outputting colors.
|
2014-08-13 21:30:25 +04:00
|
|
|
ForceColors bool
|
|
|
|
DisableColors bool
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
|
2014-05-24 18:46:03 +04:00
|
|
|
b := &bytes.Buffer{}
|
2014-03-11 03:22:08 +04:00
|
|
|
|
2014-07-27 06:22:39 +04:00
|
|
|
prefixFieldClashes(entry)
|
2014-07-27 05:26:04 +04:00
|
|
|
|
2014-08-13 21:30:25 +04:00
|
|
|
if (f.ForceColors || IsTerminal()) && !DisableColors {
|
2014-03-14 21:01:06 +04:00
|
|
|
levelText := strings.ToUpper(entry.Data["level"].(string))[0:4]
|
2014-03-11 03:22:08 +04:00
|
|
|
|
2014-03-14 21:01:06 +04:00
|
|
|
levelColor := blue
|
|
|
|
|
|
|
|
if entry.Data["level"] == "warning" {
|
|
|
|
levelColor = yellow
|
2014-03-16 07:34:03 +04:00
|
|
|
} else if entry.Data["level"] == "error" ||
|
|
|
|
entry.Data["level"] == "fatal" ||
|
2014-03-14 21:01:06 +04:00
|
|
|
entry.Data["level"] == "panic" {
|
|
|
|
levelColor = red
|
|
|
|
}
|
2014-03-11 03:22:08 +04:00
|
|
|
|
2014-05-24 18:46:03 +04:00
|
|
|
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d] %-44s ", levelColor, levelText, miniTS(), entry.Data["msg"])
|
2014-03-11 03:22:08 +04:00
|
|
|
|
2014-03-14 21:01:06 +04:00
|
|
|
keys := make([]string, 0)
|
|
|
|
for k, _ := range entry.Data {
|
|
|
|
if k != "level" && k != "time" && k != "msg" {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
2014-03-14 21:01:06 +04:00
|
|
|
sort.Strings(keys)
|
|
|
|
for _, k := range keys {
|
|
|
|
v := entry.Data[k]
|
2014-05-24 18:46:03 +04:00
|
|
|
fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=%v", levelColor, k, v)
|
2014-03-14 21:01:06 +04:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-24 18:46:03 +04:00
|
|
|
f.AppendKeyValue(b, "time", entry.Data["time"].(string))
|
|
|
|
f.AppendKeyValue(b, "level", entry.Data["level"].(string))
|
|
|
|
f.AppendKeyValue(b, "msg", entry.Data["msg"].(string))
|
2014-03-14 21:01:06 +04:00
|
|
|
|
|
|
|
for key, value := range entry.Data {
|
|
|
|
if key != "time" && key != "level" && key != "msg" {
|
2014-05-24 18:46:03 +04:00
|
|
|
f.AppendKeyValue(b, key, value)
|
2014-03-14 21:01:06 +04:00
|
|
|
}
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-24 18:46:03 +04:00
|
|
|
b.WriteByte('\n')
|
|
|
|
return b.Bytes(), nil
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
2014-03-14 21:01:06 +04:00
|
|
|
|
2014-05-24 18:46:03 +04:00
|
|
|
func (f *TextFormatter) AppendKeyValue(b *bytes.Buffer, key, value interface{}) {
|
2014-03-14 21:32:49 +04:00
|
|
|
if _, ok := value.(string); ok {
|
2014-05-24 18:46:03 +04:00
|
|
|
fmt.Fprintf(b, "%v=%q ", key, value)
|
2014-03-14 21:32:49 +04:00
|
|
|
} else {
|
2014-05-24 18:46:03 +04:00
|
|
|
fmt.Fprintf(b, "%v=%v ", key, value)
|
2014-03-14 21:32:49 +04:00
|
|
|
}
|
2014-03-14 21:01:06 +04:00
|
|
|
}
|