logrus.go 4.7 KB

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