Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修正PostgreSQL和MySQL的日志不能正确打印到database.log的问题 #1186

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions dice/model/database/mysql.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package database

import (
"log"
"os"
"time"

"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
Expand All @@ -15,14 +11,9 @@ import (
func MySQLDBInit(dsn string) (*gorm.DB, error) {
// 构建 MySQL DSN (Data Source Name)
// 使用 GORM 连接 MySQL
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Info, // 记录所有SQL操作
Colorful: true, // 是否启用彩色打印
},
)})
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
// 注意,这里虽然是Info,但实际上打印就变成了Debug.
Logger: logger.Default.LogMode(logger.Info)})
if err != nil {
return nil, err
}
Expand Down
14 changes: 2 additions & 12 deletions dice/model/database/pgsql.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package database

import (
"log"
"os"
"time"

"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
Expand All @@ -17,14 +13,8 @@ func PostgresDBInit(dsn string) (*gorm.DB, error) {

// 使用 GORM 连接 PostgreSQL
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Info, // 记录所有SQL操作
Colorful: true, // 是否启用彩色打印
},
),
// 注意,这里虽然是Info,但实际上打印就变成了Debug.
Logger: logger.Default.LogMode(logger.Info),
})
if err != nil {
return nil, err
Expand Down
Loading