Debian Codegraph @

vendor/github.com/sirupsen/logrus/formatter.go

raw
1 package logrus
2
3 import "time"
4
5 const defaultTimestampFormat = time.RFC3339
6
7 // The Formatter interface is used to implement a custom Formatter. It takes an
8 // `Entry`. It exposes all the fields, including the default ones:
9 //
10 // * `entry.Data["msg"]`. The message passed from Info, Warn, Error ..
11 // * `entry.Data["time"]`. The timestamp.
12 // * `entry.Data["level"]. The level the entry was logged at.
13 //
14 // Any additional fields added with `WithField` or `WithFields` are also in
15 // `entry.Data`. Format is expected to return an array of bytes which are then
16 // logged to `logger.Out`.
17 type Formatter interface {
18 Format(*Entry) ([]byte, error)
19 }
20
21 // This is to not silently overwrite `time`, `msg` and `level` fields when
22 // dumping it. If this code wasn't there doing:
23 //
24 // logrus.WithField("level", 1).Info("hello")
25 //
26 // Would just silently drop the user provided level. Instead with this code
27 // it'll logged as:
28 //
29 // {"level": "info", "fields.level": 1, "msg": "hello", "time": "..."}
30 //
31 // It's not exported because it's still using Data in an opinionated way. It's to
32 // avoid code duplication between the two default formatters.
33 func prefixFieldClashes(data Fields, fieldMap FieldMap) {
34 timeKey := fieldMap.resolve(FieldKeyTime)
35 if t, ok := data[timeKey]; ok {
36 data["fields."+timeKey] = t
37 delete(data, timeKey)
38 }
39
40 msgKey := fieldMap.resolve(FieldKeyMsg)
41 if m, ok := data[msgKey]; ok {
42 data["fields."+msgKey] = m
43 delete(data, msgKey)
44 }
45
46 levelKey := fieldMap.resolve(FieldKeyLevel)
47 if l, ok := data[levelKey]; ok {
48 data["fields."+levelKey] = l
49 delete(data, levelKey)
50 }
51 }