exported.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 creates 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. // TraceFn logs a message from a func at level Trace on the standard logger.
  113. func TraceFn(fn LogFunction) {
  114. std.TraceFn(fn)
  115. }
  116. // DebugFn logs a message from a func at level Debug on the standard logger.
  117. func DebugFn(fn LogFunction) {
  118. std.DebugFn(fn)
  119. }
  120. // PrintFn logs a message from a func at level Info on the standard logger.
  121. func PrintFn(fn LogFunction) {
  122. std.PrintFn(fn)
  123. }
  124. // InfoFn logs a message from a func at level Info on the standard logger.
  125. func InfoFn(fn LogFunction) {
  126. std.InfoFn(fn)
  127. }
  128. // WarnFn logs a message from a func at level Warn on the standard logger.
  129. func WarnFn(fn LogFunction) {
  130. std.WarnFn(fn)
  131. }
  132. // WarningFn logs a message from a func at level Warn on the standard logger.
  133. func WarningFn(fn LogFunction) {
  134. std.WarningFn(fn)
  135. }
  136. // ErrorFn logs a message from a func at level Error on the standard logger.
  137. func ErrorFn(fn LogFunction) {
  138. std.ErrorFn(fn)
  139. }
  140. // PanicFn logs a message from a func at level Panic on the standard logger.
  141. func PanicFn(fn LogFunction) {
  142. std.PanicFn(fn)
  143. }
  144. // FatalFn logs a message from a func at level Fatal on the standard logger then the process will exit with status set to 1.
  145. func FatalFn(fn LogFunction) {
  146. std.FatalFn(fn)
  147. }
  148. // Tracef logs a message at level Trace on the standard logger.
  149. func Tracef(format string, args ...interface{}) {
  150. std.Tracef(format, args...)
  151. }
  152. // Debugf logs a message at level Debug on the standard logger.
  153. func Debugf(format string, args ...interface{}) {
  154. std.Debugf(format, args...)
  155. }
  156. // Printf logs a message at level Info on the standard logger.
  157. func Printf(format string, args ...interface{}) {
  158. std.Printf(format, args...)
  159. }
  160. // Infof logs a message at level Info on the standard logger.
  161. func Infof(format string, args ...interface{}) {
  162. std.Infof(format, args...)
  163. }
  164. // Warnf logs a message at level Warn on the standard logger.
  165. func Warnf(format string, args ...interface{}) {
  166. std.Warnf(format, args...)
  167. }
  168. // Warningf logs a message at level Warn on the standard logger.
  169. func Warningf(format string, args ...interface{}) {
  170. std.Warningf(format, args...)
  171. }
  172. // Errorf logs a message at level Error on the standard logger.
  173. func Errorf(format string, args ...interface{}) {
  174. std.Errorf(format, args...)
  175. }
  176. // Panicf logs a message at level Panic on the standard logger.
  177. func Panicf(format string, args ...interface{}) {
  178. std.Panicf(format, args...)
  179. }
  180. // Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
  181. func Fatalf(format string, args ...interface{}) {
  182. std.Fatalf(format, args...)
  183. }
  184. // Traceln logs a message at level Trace on the standard logger.
  185. func Traceln(args ...interface{}) {
  186. std.Traceln(args...)
  187. }
  188. // Debugln logs a message at level Debug on the standard logger.
  189. func Debugln(args ...interface{}) {
  190. std.Debugln(args...)
  191. }
  192. // Println logs a message at level Info on the standard logger.
  193. func Println(args ...interface{}) {
  194. std.Println(args...)
  195. }
  196. // Infoln logs a message at level Info on the standard logger.
  197. func Infoln(args ...interface{}) {
  198. std.Infoln(args...)
  199. }
  200. // Warnln logs a message at level Warn on the standard logger.
  201. func Warnln(args ...interface{}) {
  202. std.Warnln(args...)
  203. }
  204. // Warningln logs a message at level Warn on the standard logger.
  205. func Warningln(args ...interface{}) {
  206. std.Warningln(args...)
  207. }
  208. // Errorln logs a message at level Error on the standard logger.
  209. func Errorln(args ...interface{}) {
  210. std.Errorln(args...)
  211. }
  212. // Panicln logs a message at level Panic on the standard logger.
  213. func Panicln(args ...interface{}) {
  214. std.Panicln(args...)
  215. }
  216. // Fatalln logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
  217. func Fatalln(args ...interface{}) {
  218. std.Fatalln(args...)
  219. }