chore(db): increase GORM slow SQL threshold to 500ms

This commit is contained in:
duorameng
2026-05-22 09:41:50 +08:00
parent 020f2cd583
commit b6fd889661
+13 -1
View File
@@ -2,6 +2,8 @@ package database
import ( import (
"fmt" "fmt"
"log"
"os"
"time" "time"
"github.com/engigu/baihu-panel/internal/logger" "github.com/engigu/baihu-panel/internal/logger"
@@ -58,8 +60,18 @@ func Init(cfg *Config) error {
return fmt.Errorf("unsupported database type: %s", cfg.Type) return fmt.Errorf("unsupported database type: %s", cfg.Type)
} }
newLogger := gormlogger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
gormlogger.Config{
SlowThreshold: time.Millisecond * 500, // 慢 SQL 阈值,默认是 200ms,这里改为 500ms
LogLevel: gormlogger.Warn, // 日志级别
IgnoreRecordNotFoundError: true, // 忽略 ErrRecordNotFound(找不到记录)错误
Colorful: true, // 禁用彩色打印
},
)
DB, err = gorm.Open(dialector, &gorm.Config{ DB, err = gorm.Open(dialector, &gorm.Config{
Logger: gormlogger.Default.LogMode(gormlogger.Warn), Logger: newLogger,
NowFunc: func() time.Time { NowFunc: func() time.Time {
return time.Now().In(loc) return time.Now().In(loc)
}, },