Skip to content

Commit

Permalink
fixed: 修复sql日志无法加入统一zap输出的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
piexlMax(奇淼 committed Dec 25, 2024
1 parent cb23254 commit 8b93ede
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
4 changes: 1 addition & 3 deletions server/initialize/internal/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"log"
"os"
"time"
)

Expand All @@ -34,7 +32,7 @@ func (g *_gorm) Config(prefix string, singular bool) *gorm.Config {
general = global.GVA_CONFIG.Mysql.GeneralDB
}
return &gorm.Config{
Logger: logger.New(NewWriter(general, log.New(os.Stdout, "\r\n", log.LstdFlags)), logger.Config{
Logger: logger.New(NewWriter(general), logger.Config{
SlowThreshold: 200 * time.Millisecond,
LogLevel: general.LogLevel(),
Colorful: true,
Expand Down
16 changes: 8 additions & 8 deletions server/initialize/internal/gorm_logger_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package internal
import (
"fmt"
"github.com/flipped-aurora/gin-vue-admin/server/config"
"go.uber.org/zap"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"gorm.io/gorm/logger"
)

Expand All @@ -12,24 +12,24 @@ type Writer struct {
writer logger.Writer
}

func NewWriter(config config.GeneralDB, writer logger.Writer) *Writer {
return &Writer{config: config, writer: writer}
func NewWriter(config config.GeneralDB) *Writer {
return &Writer{config: config}
}

// Printf 格式化打印日志
func (c *Writer) Printf(message string, data ...any) {
if c.config.LogZap {
switch c.config.LogLevel() {
case logger.Silent:
zap.L().Debug(fmt.Sprintf(message, data...))
global.GVA_LOG.Debug(fmt.Sprintf(message, data...))
case logger.Error:
zap.L().Error(fmt.Sprintf(message, data...))
global.GVA_LOG.Error(fmt.Sprintf(message, data...))
case logger.Warn:
zap.L().Warn(fmt.Sprintf(message, data...))
global.GVA_LOG.Warn(fmt.Sprintf(message, data...))
case logger.Info:
zap.L().Info(fmt.Sprintf(message, data...))
global.GVA_LOG.Info(fmt.Sprintf(message, data...))
default:
zap.L().Info(fmt.Sprintf(message, data...))
global.GVA_LOG.Info(fmt.Sprintf(message, data...))
}
return
}
Expand Down

0 comments on commit 8b93ede

Please sign in to comment.