exported.go 5.9 KB

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