2014-03-11 03:22:08 +04:00
|
|
|
package logrus
|
|
|
|
|
2015-04-04 03:25:25 +03:00
|
|
|
import "time"
|
|
|
|
|
2017-08-03 03:21:18 +03:00
|
|
|
// defaultTimestampFormat is YYYY-mm-DDTHH:MM:SS-TZ
|
|
|
|
const defaultTimestampFormat = time.RFC3339
|
2015-04-04 03:25:25 +03:00
|
|
|
|
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
|
|
|
|
2016-12-06 23:53:21 +03:00
|
|
|
// This is to not silently overwrite `time`, `msg`, `func` and `level` fields when
|
2014-07-27 05:26:04 +04:00
|
|
|
// 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": "..."}
|
|
|
|
//
|
2014-10-16 16:39:13 +04:00
|
|
|
// It's not exported because it's still using Data in an opinionated way. It's to
|
2014-07-27 05:26:04 +04:00
|
|
|
// avoid code duplication between the two default formatters.
|
2016-12-01 01:07:10 +03:00
|
|
|
func prefixFieldClashes(data Fields, reportCaller bool) {
|
2015-11-21 19:51:52 +03:00
|
|
|
if t, ok := data["time"]; ok {
|
|
|
|
data["fields.time"] = t
|
2014-07-27 05:26:04 +04:00
|
|
|
}
|
|
|
|
|
2015-11-21 19:51:52 +03:00
|
|
|
if m, ok := data["msg"]; ok {
|
|
|
|
data["fields.msg"] = m
|
2014-07-27 05:26:04 +04:00
|
|
|
}
|
|
|
|
|
2015-11-21 19:51:52 +03:00
|
|
|
if l, ok := data["level"]; ok {
|
|
|
|
data["fields.level"] = l
|
2014-07-27 05:26:04 +04:00
|
|
|
}
|
2016-11-26 06:02:56 +03:00
|
|
|
|
2016-12-06 23:53:21 +03:00
|
|
|
// If reportCaller is not set, 'func' will not conflict.
|
2016-12-01 01:07:10 +03:00
|
|
|
if reportCaller {
|
2016-12-06 23:53:21 +03:00
|
|
|
if l, ok := data["func"]; ok {
|
|
|
|
data["fields.func"] = l
|
2016-11-26 06:02:56 +03:00
|
|
|
}
|
|
|
|
}
|
2014-07-27 05:26:04 +04:00
|
|
|
}
|