forked from mirror/logrus
Added resolve method to clean up Format
This commit is contained in:
parent
2173899f8f
commit
b2c6f8aa8b
|
@ -5,6 +5,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type fieldKey string
|
||||||
|
|
||||||
|
const (
|
||||||
|
DefaultKeyMsg = "msg"
|
||||||
|
DefaultKeyLevel = "level"
|
||||||
|
DefaultKeyTime = "time"
|
||||||
|
)
|
||||||
|
|
||||||
type JSONFormatter struct {
|
type JSONFormatter struct {
|
||||||
// TimestampFormat sets the format used for marshaling timestamps.
|
// TimestampFormat sets the format used for marshaling timestamps.
|
||||||
TimestampFormat string
|
TimestampFormat string
|
||||||
|
@ -32,24 +40,9 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
timestampFormat = DefaultTimestampFormat
|
timestampFormat = DefaultTimestampFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
timeKey := f.TimeKey
|
data[f.resolveKey(f.TimeKey, DefaultKeyTime)] = entry.Time.Format(timestampFormat)
|
||||||
if timeKey == "" {
|
data[f.resolveKey(f.MessageKey, DefaultKeyMsg)] = entry.Message
|
||||||
timeKey = "time"
|
data[f.resolveKey(f.LevelKey, DefaultKeyLevel)] = entry.Level.String()
|
||||||
}
|
|
||||||
|
|
||||||
messageKey := f.MessageKey
|
|
||||||
if messageKey == "" {
|
|
||||||
messageKey = "msg"
|
|
||||||
}
|
|
||||||
|
|
||||||
levelKey := f.LevelKey
|
|
||||||
if levelKey == "" {
|
|
||||||
levelKey = "level"
|
|
||||||
}
|
|
||||||
|
|
||||||
data[timeKey] = entry.Time.Format(timestampFormat)
|
|
||||||
data[messageKey] = entry.Message
|
|
||||||
data[levelKey] = entry.Level.String()
|
|
||||||
|
|
||||||
serialized, err := json.Marshal(data)
|
serialized, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -57,3 +50,10 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
}
|
}
|
||||||
return append(serialized, '\n'), nil
|
return append(serialized, '\n'), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *JSONFormatter) resolveKey(key, defaultKey string) string {
|
||||||
|
if len(key) > 0 {
|
||||||
|
return key
|
||||||
|
}
|
||||||
|
return defaultKey
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue