gorm_logger_writer.go 992 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package internal
  2. import (
  3. "fmt"
  4. "github.com/flipped-aurora/gin-vue-admin/server/config"
  5. "github.com/flipped-aurora/gin-vue-admin/server/global"
  6. "gorm.io/gorm/logger"
  7. )
  8. type Writer struct {
  9. config config.GeneralDB
  10. writer logger.Writer
  11. }
  12. func NewWriter(config config.GeneralDB) *Writer {
  13. return &Writer{config: config}
  14. }
  15. // Printf 格式化打印日志
  16. func (c *Writer) Printf(message string, data ...any) {
  17. // 当有日志时候均需要输出到控制台
  18. fmt.Printf(message, data...)
  19. // 当开启了zap的情况,会打印到日志记录
  20. if c.config.LogZap {
  21. switch c.config.LogLevel() {
  22. case logger.Silent:
  23. global.GVA_LOG.Debug(fmt.Sprintf(message, data...))
  24. case logger.Error:
  25. global.GVA_LOG.Error(fmt.Sprintf(message, data...))
  26. case logger.Warn:
  27. global.GVA_LOG.Warn(fmt.Sprintf(message, data...))
  28. case logger.Info:
  29. global.GVA_LOG.Info(fmt.Sprintf(message, data...))
  30. default:
  31. global.GVA_LOG.Info(fmt.Sprintf(message, data...))
  32. }
  33. return
  34. }
  35. }