logger.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package logger
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "gorm.io/gorm/logger"
  7. "gorm.io/gorm/utils"
  8. loggerCore "gogs.baozhida.cn/zoie/OAuth-core/logger"
  9. )
  10. // Colors
  11. const (
  12. Reset = "\033[0m"
  13. Red = "\033[31m"
  14. Green = "\033[32m"
  15. Yellow = "\033[33m"
  16. Blue = "\033[34m"
  17. Magenta = "\033[35m"
  18. Cyan = "\033[36m"
  19. White = "\033[37m"
  20. BlueBold = "\033[34;1m"
  21. MagentaBold = "\033[35;1m"
  22. RedBold = "\033[31;1m"
  23. YellowBold = "\033[33;1m"
  24. )
  25. type gormLogger struct {
  26. logger.Config
  27. infoStr, warnStr, errStr string
  28. traceStr, traceErrStr, traceWarnStr string
  29. }
  30. func (l *gormLogger) getLogger(ctx context.Context) loggerCore.Logger {
  31. requestId := ctx.Value("X-Request-Id")
  32. if requestId != nil {
  33. return loggerCore.DefaultLogger.Fields(map[string]interface{}{
  34. "x-request-id": requestId,
  35. })
  36. }
  37. return loggerCore.DefaultLogger
  38. }
  39. // LogMode log mode
  40. func (l *gormLogger) LogMode(level logger.LogLevel) logger.Interface {
  41. newLogger := *l
  42. newLogger.LogLevel = level
  43. return &newLogger
  44. }
  45. // Info print info
  46. func (l gormLogger) Info(ctx context.Context, msg string, data ...interface{}) {
  47. if l.LogLevel >= logger.Info {
  48. //l.Printf(l.infoStr+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
  49. log := l.getLogger(ctx)
  50. log.Logf(loggerCore.InfoLevel, l.infoStr+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
  51. }
  52. }
  53. // Warn print warn messages
  54. func (l gormLogger) Warn(ctx context.Context, msg string, data ...interface{}) {
  55. if l.LogLevel >= logger.Warn {
  56. //l.Printf(l.warnStr+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
  57. log := l.getLogger(ctx)
  58. log.Logf(loggerCore.WarnLevel, l.warnStr+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
  59. }
  60. }
  61. // Error print error messages
  62. func (l gormLogger) Error(ctx context.Context, msg string, data ...interface{}) {
  63. if l.LogLevel >= logger.Error {
  64. //l.Printf(l.errStr+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
  65. log := l.getLogger(ctx)
  66. log.Logf(loggerCore.ErrorLevel, l.errStr+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
  67. }
  68. }
  69. // Trace print sql message
  70. func (l gormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
  71. if l.LogLevel > logger.Silent {
  72. log := l.getLogger(ctx)
  73. elapsed := time.Since(begin)
  74. switch {
  75. case err != nil && l.LogLevel >= logger.Error:
  76. sql, rows := fc()
  77. if rows == -1 {
  78. //l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
  79. log.Logf(loggerCore.TraceLevel, l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
  80. } else {
  81. //l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
  82. log.Logf(loggerCore.TraceLevel, l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
  83. }
  84. case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && l.LogLevel >= logger.Warn:
  85. sql, rows := fc()
  86. slowLog := fmt.Sprintf("SLOW SQL >= %v", l.SlowThreshold)
  87. if rows == -1 {
  88. //l.Printf(l.traceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, "-", sql)
  89. log.Logf(loggerCore.TraceLevel, l.traceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, "-", sql)
  90. } else {
  91. //l.Printf(l.traceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, rows, sql)
  92. log.Logf(loggerCore.TraceLevel, l.traceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, rows, sql)
  93. }
  94. case l.LogLevel == logger.Info:
  95. sql, rows := fc()
  96. if rows == -1 {
  97. //l.Printf(l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
  98. log.Logf(loggerCore.TraceLevel, l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
  99. } else {
  100. //l.Printf(l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
  101. log.Logf(loggerCore.TraceLevel, l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
  102. }
  103. }
  104. }
  105. }
  106. type traceRecorder struct {
  107. logger.Interface
  108. BeginAt time.Time
  109. SQL string
  110. RowsAffected int64
  111. Err error
  112. }
  113. func (l traceRecorder) New() *traceRecorder {
  114. return &traceRecorder{Interface: l.Interface, BeginAt: time.Now()}
  115. }
  116. func (l *traceRecorder) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
  117. l.BeginAt = begin
  118. l.SQL, l.RowsAffected = fc()
  119. l.Err = err
  120. }
  121. func New(config logger.Config) logger.Interface {
  122. var (
  123. infoStr = "%s\n[info] "
  124. warnStr = "%s\n[warn] "
  125. errStr = "%s\n[error] "
  126. traceStr = "%s [%.3fms] [rows:%v] %s"
  127. traceWarnStr = "%s %s [%.3fms] [rows:%v] %s"
  128. traceErrStr = "%s %s [%.3fms] [rows:%v] %s"
  129. )
  130. if config.Colorful {
  131. infoStr = Green + "%s " + Reset + Green + "[info] " + Reset
  132. warnStr = BlueBold + "%s " + Reset + Magenta + "[warn] " + Reset
  133. errStr = Magenta + "%s " + Reset + Red + "[error] " + Reset
  134. traceStr = Green + "%s " + Reset + Yellow + "[%.3fms] " + BlueBold + "[rows:%v]" + Reset + " %s"
  135. traceWarnStr = Green + "%s " + Yellow + "%s " + Reset + RedBold + "[%.3fms] " + Yellow + "[rows:%v]" + Magenta + " %s" + Reset
  136. traceErrStr = RedBold + "%s " + MagentaBold + "%s " + Reset + Yellow + "[%.3fms] " + BlueBold + "[rows:%v]" + Reset + " %s"
  137. }
  138. return &gormLogger{
  139. Config: config,
  140. infoStr: infoStr,
  141. warnStr: warnStr,
  142. errStr: errStr,
  143. traceStr: traceStr,
  144. traceWarnStr: traceWarnStr,
  145. traceErrStr: traceErrStr,
  146. }
  147. }