slog.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package slog
  2. import (
  3. "os"
  4. "strings"
  5. logrus_stack "github.com/Gurpartap/logrus-stack"
  6. "github.com/mattn/go-colorable"
  7. "github.com/sirupsen/logrus"
  8. )
  9. type logger struct {
  10. *logrus.Logger
  11. }
  12. var rootLogger *logger
  13. func init() {
  14. rootLogger = newRootLogger()
  15. logrus.AddHook(logrus_stack.NewHook(nil, []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel, logrus.WarnLevel}))
  16. logrus.SetLevel(logrus.InfoLevel)
  17. }
  18. func newRootLogger() *logger {
  19. switch strings.ToLower(os.Getenv("LOG_LEVEL")) {
  20. case "panic":
  21. logrus.SetLevel(logrus.PanicLevel)
  22. case "fatal":
  23. logrus.SetLevel(logrus.FatalLevel)
  24. case "error":
  25. logrus.SetLevel(logrus.ErrorLevel)
  26. case "warn", "warning":
  27. logrus.SetLevel(logrus.WarnLevel)
  28. case "info":
  29. logrus.SetLevel(logrus.InfoLevel)
  30. case "debug":
  31. logrus.SetLevel(logrus.DebugLevel)
  32. default:
  33. logrus.SetLevel(logrus.InfoLevel)
  34. }
  35. logrus.SetOutput(colorable.NewColorableStdout())
  36. logrus.SetFormatter(newFormatter(true))
  37. return &logger{logrus.StandardLogger()}
  38. }
  39. // SetLevelToDebug func
  40. func SetLevelToDebug() {
  41. logrus.SetLevel(logrus.DebugLevel)
  42. }
  43. // SetLevelToInfo func
  44. func SetLevelToInfo() {
  45. logrus.SetLevel(logrus.InfoLevel)
  46. }
  47. // SetLevelToWarn func
  48. func SetLevelToWarn() {
  49. logrus.SetLevel(logrus.WarnLevel)
  50. }
  51. // Debugf func
  52. func Debugf(format string, args ...interface{}) {
  53. if rootLogger.Level >= logrus.DebugLevel {
  54. if len(args) == 1 {
  55. if fx, ok := args[0].(func() interface{}); ok {
  56. rootLogger.Debugf(format, fx())
  57. return
  58. }
  59. if fx, ok := args[0].(func() []interface{}); ok {
  60. rootLogger.Debugf(format, fx()...)
  61. return
  62. }
  63. }
  64. rootLogger.Debugf(format, args...)
  65. }
  66. }
  67. // Infof func
  68. func Infof(format string, args ...interface{}) {
  69. if rootLogger.Level >= logrus.InfoLevel {
  70. if len(args) == 1 {
  71. if fx, ok := args[0].(func() interface{}); ok {
  72. rootLogger.Infof(format, fx())
  73. return
  74. }
  75. if fx, ok := args[0].(func() []interface{}); ok {
  76. rootLogger.Infof(format, fx()...)
  77. return
  78. }
  79. }
  80. rootLogger.Infof(format, args...)
  81. }
  82. }
  83. // Warnf func
  84. func Warnf(format string, args ...interface{}) {
  85. if len(args) == 1 {
  86. if fx, ok := args[0].(func() interface{}); ok {
  87. rootLogger.Warnf(format, fx())
  88. return
  89. }
  90. if fx, ok := args[0].(func() []interface{}); ok {
  91. rootLogger.Warnf(format, fx()...)
  92. return
  93. }
  94. }
  95. rootLogger.Warnf(format, args...)
  96. }
  97. // Errorf func
  98. func Errorf(format string, args ...interface{}) {
  99. if len(args) == 1 {
  100. if fx, ok := args[0].(func() interface{}); ok {
  101. rootLogger.Errorf(format, fx())
  102. return
  103. }
  104. if fx, ok := args[0].(func() []interface{}); ok {
  105. rootLogger.Errorf(format, fx()...)
  106. return
  107. }
  108. }
  109. rootLogger.Errorf(format, args...)
  110. }
  111. // Panicf func
  112. func Panicf(format string, args ...interface{}) {
  113. if len(args) == 1 {
  114. if fx, ok := args[0].(func() interface{}); ok {
  115. rootLogger.Panicf(format, fx())
  116. return
  117. }
  118. if fx, ok := args[0].(func() []interface{}); ok {
  119. rootLogger.Panicf(format, fx()...)
  120. return
  121. }
  122. }
  123. rootLogger.Panicf(format, args...)
  124. }
  125. // Debug func
  126. func Debug(args ...interface{}) {
  127. if rootLogger.Level >= logrus.DebugLevel {
  128. if len(args) == 1 {
  129. if fx, ok := args[0].(func() interface{}); ok {
  130. rootLogger.Debug(fx())
  131. return
  132. }
  133. if fx, ok := args[0].(func() []interface{}); ok {
  134. rootLogger.Debug(fx()...)
  135. return
  136. }
  137. }
  138. rootLogger.Debug(args...)
  139. }
  140. }
  141. // Info func
  142. func Info(args ...interface{}) {
  143. if rootLogger.Level >= logrus.InfoLevel {
  144. rootLogger.Info(args...)
  145. }
  146. }
  147. // Warn func
  148. func Warn(args ...interface{}) {
  149. rootLogger.Warn(args...)
  150. }
  151. // Error func
  152. func Error(args ...interface{}) {
  153. rootLogger.Error(args...)
  154. }
  155. // Panic func
  156. func Panic(args ...interface{}) {
  157. rootLogger.Panic(args...)
  158. }