2015-01-12 22:33:08 +01:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
nocolor = 0
|
|
|
|
red = 31
|
|
|
|
green = 32
|
|
|
|
yellow = 33
|
|
|
|
blue = 34
|
2015-03-24 04:09:19 +01:00
|
|
|
gray = 37
|
2015-01-12 22:33:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
baseTimestamp time.Time
|
|
|
|
isTerminal bool
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
baseTimestamp = time.Now()
|
|
|
|
isTerminal = IsTerminal()
|
|
|
|
}
|
|
|
|
|
|
|
|
func miniTS() int {
|
|
|
|
return int(time.Since(baseTimestamp) / time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TextFormatter struct {
|
|
|
|
// Set to true to bypass checking for a TTY before outputting colors.
|
2015-03-24 04:09:19 +01:00
|
|
|
ForceColors bool
|
|
|
|
|
|
|
|
// Force disabling colors.
|
2015-01-12 22:33:08 +01:00
|
|
|
DisableColors bool
|
2015-03-24 04:09:19 +01:00
|
|
|
|
|
|
|
// Disable timestamp logging. useful when output is redirected to logging
|
|
|
|
// system that already adds timestamps.
|
2015-02-05 21:00:07 +01:00
|
|
|
DisableTimestamp bool
|
2015-03-24 04:09:19 +01:00
|
|
|
|
|
|
|
// Enable logging the full timestamp when a TTY is attached instead of just
|
|
|
|
// the time passed since beginning of execution.
|
|
|
|
FullTimestamp bool
|
|
|
|
|
2015-04-10 22:54:53 +02:00
|
|
|
// TimestampFormat to use for display when a full timestamp is printed
|
|
|
|
TimestampFormat string
|
|
|
|
|
2015-03-24 04:09:19 +01:00
|
|
|
// The fields are sorted by default for a consistent output. For applications
|
|
|
|
// that log extremely frequently and don't use the JSON formatter this may not
|
|
|
|
// be desired.
|
|
|
|
DisableSorting bool
|
2015-01-12 22:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
|
2015-03-24 04:09:19 +01:00
|
|
|
var keys []string = make([]string, 0, len(entry.Data))
|
2015-01-12 22:33:08 +01:00
|
|
|
for k := range entry.Data {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
2015-03-24 04:09:19 +01:00
|
|
|
|
|
|
|
if !f.DisableSorting {
|
|
|
|
sort.Strings(keys)
|
|
|
|
}
|
2015-01-12 22:33:08 +01:00
|
|
|
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
|
2015-02-05 21:00:07 +01:00
|
|
|
prefixFieldClashes(entry.Data)
|
2015-01-12 22:33:08 +01:00
|
|
|
|
|
|
|
isColored := (f.ForceColors || isTerminal) && !f.DisableColors
|
|
|
|
|
2015-04-10 22:54:53 +02:00
|
|
|
if f.TimestampFormat == "" {
|
|
|
|
f.TimestampFormat = DefaultTimestampFormat
|
|
|
|
}
|
2015-01-12 22:33:08 +01:00
|
|
|
if isColored {
|
2015-03-24 04:09:19 +01:00
|
|
|
f.printColored(b, entry, keys)
|
2015-01-12 22:33:08 +01:00
|
|
|
} else {
|
2015-02-05 21:00:07 +01:00
|
|
|
if !f.DisableTimestamp {
|
2015-04-10 22:54:53 +02:00
|
|
|
f.appendKeyValue(b, "time", entry.Time.Format(f.TimestampFormat))
|
2015-02-05 21:00:07 +01:00
|
|
|
}
|
2015-01-12 22:33:08 +01:00
|
|
|
f.appendKeyValue(b, "level", entry.Level.String())
|
|
|
|
f.appendKeyValue(b, "msg", entry.Message)
|
|
|
|
for _, key := range keys {
|
|
|
|
f.appendKeyValue(b, key, entry.Data[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.WriteByte('\n')
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2015-03-24 04:09:19 +01:00
|
|
|
func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string) {
|
2015-01-12 22:33:08 +01:00
|
|
|
var levelColor int
|
|
|
|
switch entry.Level {
|
2015-03-24 04:09:19 +01:00
|
|
|
case DebugLevel:
|
|
|
|
levelColor = gray
|
2015-01-12 22:33:08 +01:00
|
|
|
case WarnLevel:
|
|
|
|
levelColor = yellow
|
|
|
|
case ErrorLevel, FatalLevel, PanicLevel:
|
|
|
|
levelColor = red
|
|
|
|
default:
|
|
|
|
levelColor = blue
|
|
|
|
}
|
|
|
|
|
|
|
|
levelText := strings.ToUpper(entry.Level.String())[0:4]
|
|
|
|
|
2015-03-24 04:09:19 +01:00
|
|
|
if !f.FullTimestamp {
|
|
|
|
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d] %-44s ", levelColor, levelText, miniTS(), entry.Message)
|
|
|
|
} else {
|
2015-04-10 22:54:53 +02:00
|
|
|
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %-44s ", levelColor, levelText, entry.Time.Format(f.TimestampFormat), entry.Message)
|
2015-03-24 04:09:19 +01:00
|
|
|
}
|
2015-01-12 22:33:08 +01:00
|
|
|
for _, k := range keys {
|
|
|
|
v := entry.Data[k]
|
|
|
|
fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=%v", levelColor, k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 21:00:07 +01:00
|
|
|
func needsQuoting(text string) bool {
|
|
|
|
for _, ch := range text {
|
|
|
|
if !((ch >= 'a' && ch <= 'z') ||
|
|
|
|
(ch >= 'A' && ch <= 'Z') ||
|
2015-03-24 04:09:19 +01:00
|
|
|
(ch >= '0' && ch <= '9') ||
|
2015-02-05 21:00:07 +01:00
|
|
|
ch == '-' || ch == '.') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-01-12 22:33:08 +01:00
|
|
|
func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key, value interface{}) {
|
|
|
|
switch value.(type) {
|
2015-02-05 21:00:07 +01:00
|
|
|
case string:
|
|
|
|
if needsQuoting(value.(string)) {
|
|
|
|
fmt.Fprintf(b, "%v=%s ", key, value)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(b, "%v=%q ", key, value)
|
|
|
|
}
|
|
|
|
case error:
|
|
|
|
if needsQuoting(value.(error).Error()) {
|
|
|
|
fmt.Fprintf(b, "%v=%s ", key, value)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(b, "%v=%q ", key, value)
|
|
|
|
}
|
2015-01-12 22:33:08 +01:00
|
|
|
default:
|
|
|
|
fmt.Fprintf(b, "%v=%v ", key, value)
|
|
|
|
}
|
|
|
|
}
|