You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.3 KiB

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