exported.go 6.0 KB

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