log.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package log
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "runtime"
  7. "github.com/sirupsen/logrus"
  8. )
  9. func init() {
  10. // Log as JSON instead of the default ASCII formatter.
  11. logrus.SetFormatter(&logrus.JSONFormatter{
  12. CallerPrettyfier: func(f *runtime.Frame) (string, string) {
  13. filename := path.Base(f.File)
  14. return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filename, f.Line)
  15. },
  16. })
  17. // Output to stdout instead of the default stderr
  18. // Can be any io.Writer, see below for File example
  19. logrus.SetOutput(os.Stdout)
  20. // Only log the warning severity or above.
  21. logrus.SetLevel(DebugLevel)
  22. }
  23. // These are the different logging levels. You can set the logging level to log
  24. // on your instance of logger, obtained with `logrus.New()`.
  25. const (
  26. // PanicLevel level, highest level of severity. Logs and then calls panic with the
  27. // message passed to Debug, Info, ...
  28. PanicLevel Level = iota
  29. // FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
  30. // logging level is set to Panic.
  31. FatalLevel
  32. // ErrorLevel level. Logs. Used for errors that should definitely be noted.
  33. // Commonly used for hooks to send errors to an error tracking service.
  34. ErrorLevel
  35. // WarnLevel level. Non-critical entries that deserve eyes.
  36. WarnLevel
  37. // InfoLevel level. General operational entries about what's going on inside the
  38. // application.
  39. InfoLevel
  40. // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
  41. DebugLevel
  42. // TraceLevel level. Designates finer-grained informational events than the Debug.
  43. TraceLevel
  44. )
  45. // Entry is the final or intermediate Logrus logging entry. It contains all
  46. // the fields passed with WithField{,s}. It's finally logged when Debug, Info,
  47. // Warn, Error, Fatal or Panic is called on it. These objects can be reused and
  48. // passed around as much as you wish to avoid field duplication.
  49. type Entry = logrus.Entry
  50. // Fields type, used to pass to `WithFields`.
  51. type Fields = logrus.Fields
  52. // FieldMap allows customization of the key names for default fields.
  53. type FieldMap = logrus.FieldMap
  54. // Level type
  55. type Level = logrus.Level
  56. // Logger type
  57. type Logger = logrus.Logger
  58. // JSONFormatter formats logs into parsable json
  59. type JSONFormatter struct {
  60. logrus.JSONFormatter
  61. }
  62. // TextFormatter formats logs into text
  63. type TextFormatter struct {
  64. logrus.TextFormatter
  65. }
  66. // Formatter
  67. // The Formatter interface is used to implement a custom Formatter. It takes an
  68. // `Entry`. It exposes all the fields, including the default ones:
  69. //
  70. // * `entry.Data["msg"]`. The message passed from Info, Warn, Error ..
  71. // * `entry.Data["time"]`. The timestamp.
  72. // * `entry.Data["level"]. The level the entry was logged at.
  73. //
  74. // Any additional fields added with `WithField` or `WithFields` are also in
  75. // `entry.Data`. Format is expected to return an array of bytes which are then
  76. // logged to `logger.Out`.
  77. // SetLevel ...
  78. func SetLevel(level Level) {
  79. logrus.SetLevel(level)
  80. }
  81. // NewWithFields returns a logrus Entry with fields
  82. func NewWithFields(fields Fields) *Entry {
  83. return logrus.WithFields(fields)
  84. }
  85. // NewEntry return an entry is the final or intermediate Logrus logging entry
  86. func NewEntry(logger *Logger) *Entry {
  87. return logrus.NewEntry(logger)
  88. }
  89. // Exported from logrus
  90. var (
  91. // Creates a new logger. Configuration should be set by changing `Formatter`,
  92. // `Out` and `Hooks` directly on the default logger instance. You can also just
  93. // instantiate your own:
  94. //
  95. // var log = &Logger{
  96. // Out: os.Stderr,
  97. // Formatter: new(JSONFormatter),
  98. // Level: logrus.DebugLevel,
  99. // }
  100. //
  101. // It's recommended to make this a global instance called `log`.
  102. New = logrus.New
  103. // StandardLogger default logger
  104. StandardLogger = logrus.StandardLogger
  105. // SetOutput sets the standard logger output.
  106. SetOutput = logrus.SetOutput
  107. // SetFormatter sets the standard logger formatter.
  108. SetFormatter = logrus.SetFormatter
  109. // WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key.
  110. WithError = logrus.WithError
  111. // WithField creates an entry from the standard logger and adds a field to
  112. // it. If you want multiple fields, use `WithFields`.
  113. //
  114. // Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
  115. // or Panic on the Entry it returns.
  116. WithField = logrus.WithField
  117. // WithFields creates an entry from the standard logger and adds multiple
  118. // fields to it. This is simply a helper for `WithField`, invoking it
  119. // once for each field.
  120. //
  121. // Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
  122. // or Panic on the Entry it returns.
  123. WithFields = logrus.WithFields
  124. // Debug logs a message at level Debug on the standard logger.
  125. Debug = logrus.Debug
  126. // Print logs a message at level Info on the standard logger.
  127. Print = logrus.Print
  128. // Info logs a message at level Info on the standard logger.
  129. Info = logrus.Info
  130. // Warn logs a message at level Warn on the standard logger.
  131. Warn = logrus.Warn
  132. // Warning logs a message at level Warn on the standard logger.
  133. Warning = logrus.Warning
  134. // Error logs a message at level Error on the standard logger.
  135. Error = logrus.Error
  136. // Panic logs a message at level Panic on the standard logger.
  137. Panic = logrus.Panic
  138. // Fatal logs a message at level Fatal on the standard logger.
  139. Fatal = logrus.Fatal
  140. // Debugf logs a message at level Debug on the standard logger.
  141. Debugf = logrus.Debugf
  142. // Printf logs a message at level Info on the standard logger.
  143. Printf = logrus.Printf
  144. // Infof logs a message at level Info on the standard logger.
  145. Infof = logrus.Infof
  146. // Warnf logs a message at level Warn on the standard logger.
  147. Warnf = logrus.Warnf
  148. // Warningf logs a message at level Warn on the standard logger.
  149. Warningf = logrus.Warningf
  150. // Errorf logs a message at level Error on the standard logger.
  151. Errorf = logrus.Errorf
  152. // Panicf logs a message at level Panic on the standard logger.
  153. Panicf = logrus.Panicf
  154. // Fatalf logs a message at level Fatal on the standard logger.
  155. Fatalf = logrus.Fatalf
  156. // Debugln logs a message at level Debug on the standard logger.
  157. Debugln = logrus.Debugln
  158. // Println logs a message at level Info on the standard logger.
  159. Println = logrus.Println
  160. // Infoln logs a message at level Info on the standard logger.
  161. Infoln = logrus.Infoln
  162. // Warnln logs a message at level Warn on the standard logger.
  163. Warnln = logrus.Warnln
  164. // Warningln logs a message at level Warn on the standard logger.
  165. Warningln = logrus.Warningln
  166. // Errorln logs a message at level Error on the standard logger.
  167. Errorln = logrus.Errorln
  168. // Panicln logs a message at level Panic on the standard logger.
  169. Panicln = logrus.Panicln
  170. // Fatalln logs a message at level Fatal on the standard logger.
  171. Fatalln = logrus.Fatalln
  172. )