2014-03-11 03:22:08 +04:00
|
|
|
package logrus
|
|
|
|
|
2014-07-27 05:26:04 +04:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-03-12 18:34:29 +04:00
|
|
|
// The Formatter interface is used to implement a custom Formatter. It takes an
|
|
|
|
// `Entry`. It exposes all the fields, including the default ones:
|
|
|
|
//
|
|
|
|
// * `entry.Data["msg"]`. The message passed from Info, Warn, Error ..
|
|
|
|
// * `entry.Data["time"]`. The timestamp.
|
|
|
|
// * `entry.Data["level"]. The level the entry was logged at.
|
|
|
|
//
|
|
|
|
// Any additional fields added with `WithField` or `WithFields` are also in
|
|
|
|
// `entry.Data`. Format is expected to return an array of bytes which are then
|
|
|
|
// logged to `logger.Out`.
|
2014-03-11 03:22:08 +04:00
|
|
|
type Formatter interface {
|
|
|
|
Format(*Entry) ([]byte, error)
|
|
|
|
}
|
2014-07-27 05:26:04 +04:00
|
|
|
|
|
|
|
// This is to not silently overwrite `time`, `msg` and `level` fields when
|
|
|
|
// dumping it. If this code wasn't there doing:
|
|
|
|
//
|
|
|
|
// logrus.WithField("level", 1).Info("hello")
|
|
|
|
//
|
|
|
|
// Would just silently drop the user provided level. Instead with this code
|
|
|
|
// it'll logged as:
|
|
|
|
//
|
|
|
|
// {"level": "info", "fields.level": 1, "msg": "hello", "time": "..."}
|
|
|
|
//
|
|
|
|
// It's not exported because it's still using Data in an opionated way. It's to
|
|
|
|
// avoid code duplication between the two default formatters.
|
2014-07-27 06:22:39 +04:00
|
|
|
func prefixFieldClashes(entry *Entry) {
|
2014-07-27 05:26:04 +04:00
|
|
|
_, ok := entry.Data["time"]
|
|
|
|
if ok {
|
|
|
|
entry.Data["fields.time"] = entry.Data["time"]
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.Data["time"] = entry.Time.Format(time.RFC3339)
|
|
|
|
|
|
|
|
_, ok = entry.Data["msg"]
|
|
|
|
if ok {
|
|
|
|
entry.Data["fields.msg"] = entry.Data["msg"]
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.Data["msg"] = entry.Message
|
|
|
|
|
|
|
|
_, ok = entry.Data["level"]
|
|
|
|
if ok {
|
|
|
|
entry.Data["fields.level"] = entry.Data["level"]
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.Data["level"] = entry.Level.String()
|
|
|
|
}
|