logrus.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package logrus
  2. import (
  3. "fmt"
  4. "log"
  5. "strings"
  6. )
  7. // Fields type, used to pass to `WithFields`.
  8. type Fields map[string]interface{}
  9. // Level type
  10. type Level uint8
  11. // Convert the Level to a string. E.g. PanicLevel becomes "panic".
  12. func (level Level) String() string {
  13. switch level {
  14. case DebugLevel:
  15. return "debug"
  16. case InfoLevel:
  17. return "info"
  18. case WarnLevel:
  19. return "warning"
  20. case ErrorLevel:
  21. return "error"
  22. case FatalLevel:
  23. return "fatal"
  24. case PanicLevel:
  25. return "panic"
  26. }
  27. return "unknown"
  28. }
  29. // ParseLevel takes a string level and returns the Logrus log level constant.
  30. func ParseLevel(lvl string) (Level, error) {
  31. switch strings.ToLower(lvl) {
  32. case "panic":
  33. return PanicLevel, nil
  34. case "fatal":
  35. return FatalLevel, nil
  36. case "error":
  37. return ErrorLevel, nil
  38. case "warn", "warning":
  39. return WarnLevel, nil
  40. case "info":
  41. return InfoLevel, nil
  42. case "debug":
  43. return DebugLevel, nil
  44. }
  45. var l Level
  46. return l, fmt.Errorf("not a valid logrus Level: %q", lvl)
  47. }
  48. // A constant exposing all logging levels
  49. var AllLevels = []Level{
  50. PanicLevel,
  51. FatalLevel,
  52. ErrorLevel,
  53. WarnLevel,
  54. InfoLevel,
  55. DebugLevel,
  56. }
  57. // These are the different logging levels. You can set the logging level to log
  58. // on your instance of logger, obtained with `logrus.New()`.
  59. const (
  60. // PanicLevel level, highest level of severity. Logs and then calls panic with the
  61. // message passed to Debug, Info, ...
  62. PanicLevel Level = iota
  63. // FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the
  64. // logging level is set to Panic.
  65. FatalLevel
  66. // ErrorLevel level. Logs. Used for errors that should definitely be noted.
  67. // Commonly used for hooks to send errors to an error tracking service.
  68. ErrorLevel
  69. // WarnLevel level. Non-critical entries that deserve eyes.
  70. WarnLevel
  71. // InfoLevel level. General operational entries about what's going on inside the
  72. // application.
  73. InfoLevel
  74. // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
  75. DebugLevel
  76. )
  77. // Won't compile if StdLogger can't be realized by a log.Logger
  78. var (
  79. _ StdLogger = &log.Logger{}
  80. _ StdLogger = &Entry{}
  81. _ StdLogger = &Logger{}
  82. )
  83. // StdLogger is what your logrus-enabled library should take, that way
  84. // it'll accept a stdlib logger and a logrus logger. There's no standard
  85. // interface, this is the closest we get, unfortunately.
  86. type StdLogger interface {
  87. Print(...interface{})
  88. Printf(string, ...interface{})
  89. Println(...interface{})
  90. Fatal(...interface{})
  91. Fatalf(string, ...interface{})
  92. Fatalln(...interface{})
  93. Panic(...interface{})
  94. Panicf(string, ...interface{})
  95. Panicln(...interface{})
  96. }
  97. // The FieldLogger interface generalizes the Entry and Logger types
  98. type FieldLogger interface {
  99. WithField(key string, value interface{}) *Entry
  100. WithFields(fields Fields) *Entry
  101. WithError(err error) *Entry
  102. Debugf(format string, args ...interface{})
  103. Infof(format string, args ...interface{})
  104. Printf(format string, args ...interface{})
  105. Warnf(format string, args ...interface{})
  106. Warningf(format string, args ...interface{})
  107. Errorf(format string, args ...interface{})
  108. Fatalf(format string, args ...interface{})
  109. Panicf(format string, args ...interface{})
  110. Debug(args ...interface{})
  111. Info(args ...interface{})
  112. Print(args ...interface{})
  113. Warn(args ...interface{})
  114. Warning(args ...interface{})
  115. Error(args ...interface{})
  116. Fatal(args ...interface{})
  117. Panic(args ...interface{})
  118. Debugln(args ...interface{})
  119. Infoln(args ...interface{})
  120. Println(args ...interface{})
  121. Warnln(args ...interface{})
  122. Warningln(args ...interface{})
  123. Errorln(args ...interface{})
  124. Fatalln(args ...interface{})
  125. Panicln(args ...interface{})
  126. }