logger.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package logrus
  2. import (
  3. "io"
  4. "os"
  5. "sync"
  6. )
  7. type Logger struct {
  8. // The logs are `io.Copy`'d to this in a mutex. It's common to set this to a
  9. // file, or leave it default which is `os.Stderr`. You can also set this to
  10. // something more adventorous, such as logging to Kafka.
  11. Out io.Writer
  12. // Hooks for the logger instance. These allow firing events based on logging
  13. // levels and log entries. For example, to send errors to an error tracking
  14. // service, log to StatsD or dump the core on fatal errors.
  15. Hooks LevelHooks
  16. // All log entries pass through the formatter before logged to Out. The
  17. // included formatters are `TextFormatter` and `JSONFormatter` for which
  18. // TextFormatter is the default. In development (when a TTY is attached) it
  19. // logs with colors, but to a file it wouldn't. You can easily implement your
  20. // own that implements the `Formatter` interface, see the `README` or included
  21. // formatters for examples.
  22. Formatter Formatter
  23. // The logging level the logger should log at. This is typically (and defaults
  24. // to) `logrus.Info`, which allows Info(), Warn(), Error() and Fatal() to be
  25. // logged. `logrus.Debug` is useful in
  26. Level Level
  27. // Used to sync writing to the log.
  28. mu sync.Mutex
  29. }
  30. // Creates a new logger. Configuration should be set by changing `Formatter`,
  31. // `Out` and `Hooks` directly on the default logger instance. You can also just
  32. // instantiate your own:
  33. //
  34. // var log = &Logger{
  35. // Out: os.Stderr,
  36. // Formatter: new(JSONFormatter),
  37. // Hooks: make(LevelHooks),
  38. // Level: logrus.DebugLevel,
  39. // }
  40. //
  41. // It's recommended to make this a global instance called `log`.
  42. func New() *Logger {
  43. return &Logger{
  44. Out: os.Stderr,
  45. Formatter: new(TextFormatter),
  46. Hooks: make(LevelHooks),
  47. Level: InfoLevel,
  48. }
  49. }
  50. // Adds a field to the log entry, note that you it doesn't log until you call
  51. // Debug, Print, Info, Warn, Fatal or Panic. It only creates a log entry.
  52. // If you want multiple fields, use `WithFields`.
  53. func (logger *Logger) WithField(key string, value interface{}) *Entry {
  54. return NewEntry(logger).WithField(key, value)
  55. }
  56. // Adds a struct of fields to the log entry. All it does is call `WithField` for
  57. // each `Field`.
  58. func (logger *Logger) WithFields(fields Fields) *Entry {
  59. return NewEntry(logger).WithFields(fields)
  60. }
  61. // Add an error as single field to the log entry. All it does is call
  62. // `WithError` for the given `error`.
  63. func (logger *Logger) WithError(err error) *Entry {
  64. return NewEntry(logger).WithError(err)
  65. }
  66. func (logger *Logger) Debugf(format string, args ...interface{}) {
  67. if logger.Level >= DebugLevel {
  68. NewEntry(logger).Debugf(format, args...)
  69. }
  70. }
  71. func (logger *Logger) Infof(format string, args ...interface{}) {
  72. if logger.Level >= InfoLevel {
  73. NewEntry(logger).Infof(format, args...)
  74. }
  75. }
  76. func (logger *Logger) Printf(format string, args ...interface{}) {
  77. NewEntry(logger).Printf(format, args...)
  78. }
  79. func (logger *Logger) Warnf(format string, args ...interface{}) {
  80. if logger.Level >= WarnLevel {
  81. NewEntry(logger).Warnf(format, args...)
  82. }
  83. }
  84. func (logger *Logger) Warningf(format string, args ...interface{}) {
  85. if logger.Level >= WarnLevel {
  86. NewEntry(logger).Warnf(format, args...)
  87. }
  88. }
  89. func (logger *Logger) Errorf(format string, args ...interface{}) {
  90. if logger.Level >= ErrorLevel {
  91. NewEntry(logger).Errorf(format, args...)
  92. }
  93. }
  94. func (logger *Logger) Fatalf(format string, args ...interface{}) {
  95. if logger.Level >= FatalLevel {
  96. NewEntry(logger).Fatalf(format, args...)
  97. }
  98. os.Exit(1)
  99. }
  100. func (logger *Logger) Panicf(format string, args ...interface{}) {
  101. if logger.Level >= PanicLevel {
  102. NewEntry(logger).Panicf(format, args...)
  103. }
  104. }
  105. func (logger *Logger) Debug(args ...interface{}) {
  106. if logger.Level >= DebugLevel {
  107. NewEntry(logger).Debug(args...)
  108. }
  109. }
  110. func (logger *Logger) Info(args ...interface{}) {
  111. if logger.Level >= InfoLevel {
  112. NewEntry(logger).Info(args...)
  113. }
  114. }
  115. func (logger *Logger) Print(args ...interface{}) {
  116. NewEntry(logger).Info(args...)
  117. }
  118. func (logger *Logger) Warn(args ...interface{}) {
  119. if logger.Level >= WarnLevel {
  120. NewEntry(logger).Warn(args...)
  121. }
  122. }
  123. func (logger *Logger) Warning(args ...interface{}) {
  124. if logger.Level >= WarnLevel {
  125. NewEntry(logger).Warn(args...)
  126. }
  127. }
  128. func (logger *Logger) Error(args ...interface{}) {
  129. if logger.Level >= ErrorLevel {
  130. NewEntry(logger).Error(args...)
  131. }
  132. }
  133. func (logger *Logger) Fatal(args ...interface{}) {
  134. if logger.Level >= FatalLevel {
  135. NewEntry(logger).Fatal(args...)
  136. }
  137. os.Exit(1)
  138. }
  139. func (logger *Logger) Panic(args ...interface{}) {
  140. if logger.Level >= PanicLevel {
  141. NewEntry(logger).Panic(args...)
  142. }
  143. }
  144. func (logger *Logger) Debugln(args ...interface{}) {
  145. if logger.Level >= DebugLevel {
  146. NewEntry(logger).Debugln(args...)
  147. }
  148. }
  149. func (logger *Logger) Infoln(args ...interface{}) {
  150. if logger.Level >= InfoLevel {
  151. NewEntry(logger).Infoln(args...)
  152. }
  153. }
  154. func (logger *Logger) Println(args ...interface{}) {
  155. NewEntry(logger).Println(args...)
  156. }
  157. func (logger *Logger) Warnln(args ...interface{}) {
  158. if logger.Level >= WarnLevel {
  159. NewEntry(logger).Warnln(args...)
  160. }
  161. }
  162. func (logger *Logger) Warningln(args ...interface{}) {
  163. if logger.Level >= WarnLevel {
  164. NewEntry(logger).Warnln(args...)
  165. }
  166. }
  167. func (logger *Logger) Errorln(args ...interface{}) {
  168. if logger.Level >= ErrorLevel {
  169. NewEntry(logger).Errorln(args...)
  170. }
  171. }
  172. func (logger *Logger) Fatalln(args ...interface{}) {
  173. if logger.Level >= FatalLevel {
  174. NewEntry(logger).Fatalln(args...)
  175. }
  176. os.Exit(1)
  177. }
  178. func (logger *Logger) Panicln(args ...interface{}) {
  179. if logger.Level >= PanicLevel {
  180. NewEntry(logger).Panicln(args...)
  181. }
  182. }