logrus.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package logrus
  2. import (
  3. "fmt"
  4. "log"
  5. )
  6. // Fields type, used to pass to `WithFields`.
  7. type Fields map[string]interface{}
  8. // Level type
  9. type Level uint8
  10. // Convert the Level to a string. E.g. PanicLevel becomes "panic".
  11. func (level Level) String() string {
  12. switch level {
  13. case DebugLevel:
  14. return "debug"
  15. case InfoLevel:
  16. return "info"
  17. case WarnLevel:
  18. return "warning"
  19. case ErrorLevel:
  20. return "error"
  21. case FatalLevel:
  22. return "fatal"
  23. case PanicLevel:
  24. return "panic"
  25. }
  26. return "unknown"
  27. }
  28. // ParseLevel takes a string level and returns the Logrus log level constant.
  29. func ParseLevel(lvl string) (Level, error) {
  30. switch lvl {
  31. case "panic":
  32. return PanicLevel, nil
  33. case "fatal":
  34. return FatalLevel, nil
  35. case "error":
  36. return ErrorLevel, nil
  37. case "warn", "warning":
  38. return WarnLevel, nil
  39. case "info":
  40. return InfoLevel, nil
  41. case "debug":
  42. return DebugLevel, nil
  43. }
  44. var l Level
  45. return l, fmt.Errorf("not a valid logrus Level: %q", lvl)
  46. }
  47. // These are the different logging levels. You can set the logging level to log
  48. // on your instance of logger, obtained with `logrus.New()`.
  49. const (
  50. // PanicLevel level, highest level of severity. Logs and then calls panic with the
  51. // message passed to Debug, Info, ...
  52. PanicLevel Level = iota
  53. // FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the
  54. // logging level is set to Panic.
  55. FatalLevel
  56. // ErrorLevel level. Logs. Used for errors that should definitely be noted.
  57. // Commonly used for hooks to send errors to an error tracking service.
  58. ErrorLevel
  59. // WarnLevel level. Non-critical entries that deserve eyes.
  60. WarnLevel
  61. // InfoLevel level. General operational entries about what's going on inside the
  62. // application.
  63. InfoLevel
  64. // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
  65. DebugLevel
  66. )
  67. // Won't compile if StdLogger can't be realized by a log.Logger
  68. var (
  69. _ StdLogger = &log.Logger{}
  70. _ StdLogger = &Entry{}
  71. _ StdLogger = &Logger{}
  72. )
  73. // StdLogger is what your logrus-enabled library should take, that way
  74. // it'll accept a stdlib logger and a logrus logger. There's no standard
  75. // interface, this is the closest we get, unfortunately.
  76. type StdLogger interface {
  77. Print(...interface{})
  78. Printf(string, ...interface{})
  79. Println(...interface{})
  80. Fatal(...interface{})
  81. Fatalf(string, ...interface{})
  82. Fatalln(...interface{})
  83. Panic(...interface{})
  84. Panicf(string, ...interface{})
  85. Panicln(...interface{})
  86. }