You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.3KB

  1. package loggers
  2. import (
  3. "context"
  4. "github.com/tal-tech/go-zero/core/logx"
  5. ll "gorm.io/gorm/logger"
  6. "gorm.io/gorm/utils"
  7. "time"
  8. )
  9. // LogLevel
  10. type LogLevel int
  11. // Writer log writer interface
  12. type Writer interface {
  13. Printf(string, ...interface{})
  14. }
  15. // Interface logger interface
  16. type Interface interface {
  17. LogMode(LogLevel) Interface
  18. Info(context.Context, string, ...interface{})
  19. Warn(context.Context, string, ...interface{})
  20. Error(context.Context, string, ...interface{})
  21. Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)
  22. }
  23. var (
  24. //Discard = New(log.New(ioutil.Discard, "", log.LstdFlags), Config{})
  25. //Default = New(log.New(os.Stdout, "\r\n", log.LstdFlags), Config{
  26. // SlowThreshold: 100 * time.Millisecond,
  27. // LogLevel: Warn,
  28. // Colorful: true,
  29. //})
  30. //Recorder = traceRecorder{Interface: Default}
  31. )
  32. func New() ll.Interface {
  33. return &logger{}
  34. }
  35. type logger struct {
  36. Writer
  37. ll.Config
  38. infoStr, warnStr, errStr string
  39. traceStr, traceErrStr, traceWarnStr string
  40. }
  41. // LogMode log mode
  42. func (l logger) LogMode(level ll.LogLevel) ll.Interface {
  43. newlogger := l
  44. newlogger.LogLevel = level
  45. return &newlogger
  46. }
  47. // Info print info
  48. func (l logger) Info(ctx context.Context, msg string, data ...interface{}) {
  49. logx.Infof(l.infoStr+msg, data...)
  50. }
  51. // Warn print warn messages
  52. func (l logger) Warn(ctx context.Context, msg string, data ...interface{}) {
  53. logx.Infof(l.warnStr+msg, data...)
  54. }
  55. // Error print error messages
  56. func (l logger) Error(ctx context.Context, msg string, data ...interface{}) {
  57. logx.Errorf(l.errStr+msg, data...)
  58. }
  59. // Trace print sql message
  60. func (l logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
  61. if l.LogLevel > 0 {
  62. elapsed := time.Since(begin)
  63. switch {
  64. case err != nil && l.LogLevel >= ll.Error:
  65. sql, rows := fc()
  66. if rows == -1 {
  67. logx.Error(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
  68. //l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
  69. } else {
  70. logx.Error(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
  71. //l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
  72. }
  73. case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && l.LogLevel >= ll.Warn:
  74. sql, rows := fc()
  75. if rows == -1 {
  76. logx.Info(l.traceWarnStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
  77. } else {
  78. logx.Info(l.traceWarnStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
  79. }
  80. case l.LogLevel >= ll.Info:
  81. sql, rows := fc()
  82. if rows == -1 {
  83. logx.Info(l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
  84. } else {
  85. logx.Info(l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
  86. }
  87. }
  88. }
  89. }
  90. type traceRecorder struct {
  91. Interface
  92. BeginAt time.Time
  93. SQL string
  94. RowsAffected int64
  95. Err error
  96. }
  97. func (l traceRecorder) New() *traceRecorder {
  98. return &traceRecorder{Interface: l.Interface}
  99. }
  100. func (l *traceRecorder) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
  101. l.BeginAt = begin
  102. l.SQL, l.RowsAffected = fc()
  103. l.Err = err
  104. }