PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/logger.go

https://github.com/rayleyva/logrus
Go | 161 lines | 99 code | 29 blank | 33 comment | 0 complexity | 54a0c6e06544bec51ecce6a26794da74 MD5 | raw file
  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.Stdout`. 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.Debug,
  39. // }
  40. //
  41. // It's recommended to make this a global instance called `log`.
  42. func New() *Logger {
  43. return &Logger{
  44. Out: os.Stdout,
  45. Formatter: new(TextFormatter),
  46. Hooks: make(levelHooks),
  47. Level: Info,
  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. // Ff 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. func (logger *Logger) Debugf(format string, args ...interface{}) {
  62. NewEntry(logger).Debugf(format, args...)
  63. }
  64. func (logger *Logger) Infof(format string, args ...interface{}) {
  65. NewEntry(logger).Infof(format, args...)
  66. }
  67. func (logger *Logger) Printf(format string, args ...interface{}) {
  68. NewEntry(logger).Printf(format, args...)
  69. }
  70. func (logger *Logger) Warnf(format string, args ...interface{}) {
  71. NewEntry(logger).Warnf(format, args...)
  72. }
  73. func (logger *Logger) Warningf(format string, args ...interface{}) {
  74. NewEntry(logger).Warnf(format, args...)
  75. }
  76. func (logger *Logger) Errorf(format string, args ...interface{}) {
  77. NewEntry(logger).Errorf(format, args...)
  78. }
  79. func (logger *Logger) Fatalf(format string, args ...interface{}) {
  80. NewEntry(logger).Fatalf(format, args...)
  81. }
  82. func (logger *Logger) Panicf(format string, args ...interface{}) {
  83. NewEntry(logger).Panicf(format, args...)
  84. }
  85. func (logger *Logger) Debug(args ...interface{}) {
  86. NewEntry(logger).Debug(args...)
  87. }
  88. func (logger *Logger) Info(args ...interface{}) {
  89. NewEntry(logger).Info(args...)
  90. }
  91. func (logger *Logger) Print(args ...interface{}) {
  92. NewEntry(logger).Info(args...)
  93. }
  94. func (logger *Logger) Warn(args ...interface{}) {
  95. NewEntry(logger).Warn(args...)
  96. }
  97. func (logger *Logger) Warning(args ...interface{}) {
  98. NewEntry(logger).Warn(args...)
  99. }
  100. func (logger *Logger) Error(args ...interface{}) {
  101. NewEntry(logger).Error(args...)
  102. }
  103. func (logger *Logger) Fatal(args ...interface{}) {
  104. NewEntry(logger).Fatal(args...)
  105. }
  106. func (logger *Logger) Panic(args ...interface{}) {
  107. NewEntry(logger).Panic(args...)
  108. }
  109. func (logger *Logger) Debugln(args ...interface{}) {
  110. NewEntry(logger).Debugln(args...)
  111. }
  112. func (logger *Logger) Infoln(args ...interface{}) {
  113. NewEntry(logger).Infoln(args...)
  114. }
  115. func (logger *Logger) Println(args ...interface{}) {
  116. NewEntry(logger).Println(args...)
  117. }
  118. func (logger *Logger) Warnln(args ...interface{}) {
  119. NewEntry(logger).Warnln(args...)
  120. }
  121. func (logger *Logger) Warningln(args ...interface{}) {
  122. NewEntry(logger).Warnln(args...)
  123. }
  124. func (logger *Logger) Errorln(args ...interface{}) {
  125. NewEntry(logger).Errorln(args...)
  126. }
  127. func (logger *Logger) Fatalln(args ...interface{}) {
  128. NewEntry(logger).Fatalln(args...)
  129. }
  130. func (logger *Logger) Panicln(args ...interface{}) {
  131. NewEntry(logger).Panicln(args...)
  132. }