exported.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package logrus
  2. import (
  3. "io"
  4. )
  5. var (
  6. // std is the name of the standard logger in stdlib `log`
  7. std = New()
  8. )
  9. func StandardLogger() *Logger {
  10. return std
  11. }
  12. // SetOutput sets the standard logger output.
  13. func SetOutput(out io.Writer) {
  14. std.mu.Lock()
  15. defer std.mu.Unlock()
  16. std.Out = out
  17. }
  18. // SetFormatter sets the standard logger formatter.
  19. func SetFormatter(formatter Formatter) {
  20. std.mu.Lock()
  21. defer std.mu.Unlock()
  22. std.Formatter = formatter
  23. }
  24. // SetLevel sets the standard logger level.
  25. func SetLevel(level Level) {
  26. std.mu.Lock()
  27. defer std.mu.Unlock()
  28. std.Level = level
  29. }
  30. // GetLevel returns the standard logger level.
  31. func GetLevel() Level {
  32. std.mu.Lock()
  33. defer std.mu.Unlock()
  34. return std.Level
  35. }
  36. // AddHook adds a hook to the standard logger hooks.
  37. func AddHook(hook Hook) {
  38. std.mu.Lock()
  39. defer std.mu.Unlock()
  40. std.Hooks.Add(hook)
  41. }
  42. // WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key.
  43. func WithError(err error) *Entry {
  44. return std.WithField(ErrorKey, err)
  45. }
  46. // WithField creates an entry from the standard logger and adds a field to
  47. // it. If you want multiple fields, use `WithFields`.
  48. //
  49. // Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
  50. // or Panic on the Entry it returns.
  51. func WithField(key string, value interface{}) *Entry {
  52. return std.WithField(key, value)
  53. }
  54. // WithFields creates an entry from the standard logger and adds multiple
  55. // fields to it. This is simply a helper for `WithField`, invoking it
  56. // once for each field.
  57. //
  58. // Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
  59. // or Panic on the Entry it returns.
  60. func WithFields(fields Fields) *Entry {
  61. return std.WithFields(fields)
  62. }
  63. // Debug logs a message at level Debug on the standard logger.
  64. func Debug(args ...interface{}) {
  65. std.Debug(args...)
  66. }
  67. // Print logs a message at level Info on the standard logger.
  68. func Print(args ...interface{}) {
  69. std.Print(args...)
  70. }
  71. // Info logs a message at level Info on the standard logger.
  72. func Info(args ...interface{}) {
  73. std.Info(args...)
  74. }
  75. // Warn logs a message at level Warn on the standard logger.
  76. func Warn(args ...interface{}) {
  77. std.Warn(args...)
  78. }
  79. // Warning logs a message at level Warn on the standard logger.
  80. func Warning(args ...interface{}) {
  81. std.Warning(args...)
  82. }
  83. // Error logs a message at level Error on the standard logger.
  84. func Error(args ...interface{}) {
  85. std.Error(args...)
  86. }
  87. // Panic logs a message at level Panic on the standard logger.
  88. func Panic(args ...interface{}) {
  89. std.Panic(args...)
  90. }
  91. // Fatal logs a message at level Fatal on the standard logger.
  92. func Fatal(args ...interface{}) {
  93. std.Fatal(args...)
  94. }
  95. // Debugf logs a message at level Debug on the standard logger.
  96. func Debugf(format string, args ...interface{}) {
  97. std.Debugf(format, args...)
  98. }
  99. // Printf logs a message at level Info on the standard logger.
  100. func Printf(format string, args ...interface{}) {
  101. std.Printf(format, args...)
  102. }
  103. // Infof logs a message at level Info on the standard logger.
  104. func Infof(format string, args ...interface{}) {
  105. std.Infof(format, args...)
  106. }
  107. // Warnf logs a message at level Warn on the standard logger.
  108. func Warnf(format string, args ...interface{}) {
  109. std.Warnf(format, args...)
  110. }
  111. // Warningf logs a message at level Warn on the standard logger.
  112. func Warningf(format string, args ...interface{}) {
  113. std.Warningf(format, args...)
  114. }
  115. // Errorf logs a message at level Error on the standard logger.
  116. func Errorf(format string, args ...interface{}) {
  117. std.Errorf(format, args...)
  118. }
  119. // Panicf logs a message at level Panic on the standard logger.
  120. func Panicf(format string, args ...interface{}) {
  121. std.Panicf(format, args...)
  122. }
  123. // Fatalf logs a message at level Fatal on the standard logger.
  124. func Fatalf(format string, args ...interface{}) {
  125. std.Fatalf(format, args...)
  126. }
  127. // Debugln logs a message at level Debug on the standard logger.
  128. func Debugln(args ...interface{}) {
  129. std.Debugln(args...)
  130. }
  131. // Println logs a message at level Info on the standard logger.
  132. func Println(args ...interface{}) {
  133. std.Println(args...)
  134. }
  135. // Infoln logs a message at level Info on the standard logger.
  136. func Infoln(args ...interface{}) {
  137. std.Infoln(args...)
  138. }
  139. // Warnln logs a message at level Warn on the standard logger.
  140. func Warnln(args ...interface{}) {
  141. std.Warnln(args...)
  142. }
  143. // Warningln logs a message at level Warn on the standard logger.
  144. func Warningln(args ...interface{}) {
  145. std.Warningln(args...)
  146. }
  147. // Errorln logs a message at level Error on the standard logger.
  148. func Errorln(args ...interface{}) {
  149. std.Errorln(args...)
  150. }
  151. // Panicln logs a message at level Panic on the standard logger.
  152. func Panicln(args ...interface{}) {
  153. std.Panicln(args...)
  154. }
  155. // Fatalln logs a message at level Fatal on the standard logger.
  156. func Fatalln(args ...interface{}) {
  157. std.Fatalln(args...)
  158. }