logrus.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 uint32
  11. // Convert the Level to a string. E.g. PanicLevel becomes "panic".
  12. func (level Level) String() string {
  13. switch level {
  14. case TraceLevel:
  15. return "trace"
  16. case DebugLevel:
  17. return "debug"
  18. case InfoLevel:
  19. return "info"
  20. case WarnLevel:
  21. return "warning"
  22. case ErrorLevel:
  23. return "error"
  24. case FatalLevel:
  25. return "fatal"
  26. case PanicLevel:
  27. return "panic"
  28. }
  29. return "unknown"
  30. }
  31. // ParseLevel takes a string level and returns the Logrus log level constant.
  32. func ParseLevel(lvl string) (Level, error) {
  33. switch strings.ToLower(lvl) {
  34. case "panic":
  35. return PanicLevel, nil
  36. case "fatal":
  37. return FatalLevel, nil
  38. case "error":
  39. return ErrorLevel, nil
  40. case "warn", "warning":
  41. return WarnLevel, nil
  42. case "info":
  43. return InfoLevel, nil
  44. case "debug":
  45. return DebugLevel, nil
  46. case "trace":
  47. return TraceLevel, nil
  48. }
  49. var l Level
  50. return l, fmt.Errorf("not a valid logrus Level: %q", lvl)
  51. }
  52. // UnmarshalText implements encoding.TextUnmarshaler.
  53. func (level *Level) UnmarshalText(text []byte) error {
  54. l, err := ParseLevel(string(text))
  55. if err != nil {
  56. return err
  57. }
  58. *level = Level(l)
  59. return nil
  60. }
  61. // A constant exposing all logging levels
  62. var AllLevels = []Level{
  63. PanicLevel,
  64. FatalLevel,
  65. ErrorLevel,
  66. WarnLevel,
  67. InfoLevel,
  68. DebugLevel,
  69. TraceLevel,
  70. }
  71. // These are the different logging levels. You can set the logging level to log
  72. // on your instance of logger, obtained with `logrus.New()`.
  73. const (
  74. // PanicLevel level, highest level of severity. Logs and then calls panic with the
  75. // message passed to Debug, Info, ...
  76. PanicLevel Level = iota
  77. // FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
  78. // logging level is set to Panic.
  79. FatalLevel
  80. // ErrorLevel level. Logs. Used for errors that should definitely be noted.
  81. // Commonly used for hooks to send errors to an error tracking service.
  82. ErrorLevel
  83. // WarnLevel level. Non-critical entries that deserve eyes.
  84. WarnLevel
  85. // InfoLevel level. General operational entries about what's going on inside the
  86. // application.
  87. InfoLevel
  88. // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
  89. DebugLevel
  90. // TraceLevel level. Designates finer-grained informational events than the Debug.
  91. TraceLevel
  92. )
  93. // Won't compile if StdLogger can't be realized by a log.Logger
  94. var (
  95. _ StdLogger = &log.Logger{}
  96. _ StdLogger = &Entry{}
  97. _ StdLogger = &Logger{}
  98. )
  99. // StdLogger is what your logrus-enabled library should take, that way
  100. // it'll accept a stdlib logger and a logrus logger. There's no standard
  101. // interface, this is the closest we get, unfortunately.
  102. type StdLogger interface {
  103. Print(...interface{})
  104. Printf(string, ...interface{})
  105. Println(...interface{})
  106. Fatal(...interface{})
  107. Fatalf(string, ...interface{})
  108. Fatalln(...interface{})
  109. Panic(...interface{})
  110. Panicf(string, ...interface{})
  111. Panicln(...interface{})
  112. }
  113. // The FieldLogger interface generalizes the Entry and Logger types
  114. type FieldLogger interface {
  115. WithField(key string, value interface{}) *Entry
  116. WithFields(fields Fields) *Entry
  117. WithError(err error) *Entry
  118. Debugf(format string, args ...interface{})
  119. Infof(format string, args ...interface{})
  120. Printf(format string, args ...interface{})
  121. Warnf(format string, args ...interface{})
  122. Warningf(format string, args ...interface{})
  123. Errorf(format string, args ...interface{})
  124. Fatalf(format string, args ...interface{})
  125. Panicf(format string, args ...interface{})
  126. Debug(args ...interface{})
  127. Info(args ...interface{})
  128. Print(args ...interface{})
  129. Warn(args ...interface{})
  130. Warning(args ...interface{})
  131. Error(args ...interface{})
  132. Fatal(args ...interface{})
  133. Panic(args ...interface{})
  134. Debugln(args ...interface{})
  135. Infoln(args ...interface{})
  136. Println(args ...interface{})
  137. Warnln(args ...interface{})
  138. Warningln(args ...interface{})
  139. Errorln(args ...interface{})
  140. Fatalln(args ...interface{})
  141. Panicln(args ...interface{})
  142. // IsDebugEnabled() bool
  143. // IsInfoEnabled() bool
  144. // IsWarnEnabled() bool
  145. // IsErrorEnabled() bool
  146. // IsFatalEnabled() bool
  147. // IsPanicEnabled() bool
  148. }
  149. // Ext1FieldLogger (the first extension to FieldLogger) is superfluous, it is
  150. // here for consistancy. Do not use. Use Logger or Entry instead.
  151. type Ext1FieldLogger interface {
  152. FieldLogger
  153. Tracef(format string, args ...interface{})
  154. Trace(args ...interface{})
  155. Traceln(args ...interface{})
  156. }