From 54ec95048ac354ad9fd36e2ea861dda87a4f6410 Mon Sep 17 00:00:00 2001 From: kanade Date: Tue, 5 Jul 2022 12:07:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 ++-- go.sum | 19 +++++++++---------- mysql.go | 6 +++++- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 47e1697..62e4ac8 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module git.oa00.com/go/mysql go 1.16 require ( - gorm.io/driver/mysql v1.0.5 - gorm.io/gorm v1.21.8 + gorm.io/driver/mysql v1.3.4 + gorm.io/gorm v1.23.5 ) diff --git a/go.sum b/go.sum index 947825c..d635bbe 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,11 @@ -github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= -github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= -github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= -github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI= -github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= -gorm.io/driver/mysql v1.0.5 h1:WAAmvLK2rG0tCOqrf5XcLi2QUwugd4rcVJ/W3aoon9o= -gorm.io/driver/mysql v1.0.5/go.mod h1:N1OIhHAIhx5SunkMGqWbGFVeh4yTNWKmMo1GOAsohLI= -gorm.io/gorm v1.21.3/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw= -gorm.io/gorm v1.21.8 h1:2CEwZSzogdhsKPlJ9OvBKTdlWIpELXb6HbfLfMNhSYI= -gorm.io/gorm v1.21.8/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= +github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas= +github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +gorm.io/driver/mysql v1.3.4 h1:/KoBMgsUHC3bExsekDcmNYaBnfH2WNeFuXqqrqMc98Q= +gorm.io/driver/mysql v1.3.4/go.mod h1:s4Tq0KmD0yhPGHbZEwg1VPlH0vT/GBHJZorPzhcxBUE= +gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= +gorm.io/gorm v1.23.5 h1:TnlF26wScKSvknUC/Rn8t0NLLM22fypYBlvj1+aH6dM= +gorm.io/gorm v1.23.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= diff --git a/mysql.go b/mysql.go index 6c6770c..d5d54f3 100644 --- a/mysql.go +++ b/mysql.go @@ -25,6 +25,7 @@ type DbConfig struct { MaxIdleConns int MaxOpenConns int DbName string + Logger logger.Writer } type LogLevel = logger.LogLevel @@ -38,13 +39,16 @@ func InitMysql(dbConfig *DbConfig) (err error) { mysqlConfig := mysql.Config{ DSN: dsn, // DSN data source name } + if dbConfig.Logger == nil { + dbConfig.Logger = log.New(os.Stdout, "\r\n", log.LstdFlags) + } if Db, err = gorm.Open(mysql.New(mysqlConfig), &gorm.Config{ NamingStrategy: schema.NamingStrategy{ TablePrefix: dbConfig.Prefix, // 表名前缀 SingularTable: !dbConfig.SingularTable, // 使用单数表名 }, Logger: logger.New( - log.New(os.Stdout, "\r\n", log.LstdFlags), + dbConfig.Logger, logger.Config{ Colorful: !dbConfig.LogColorful, LogLevel: dbConfig.LogLevel, // Log level