From db1a04326138833c29e0417ef1e254e282cf2ca2 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Thu, 21 Jul 2022 10:56:50 +0800 Subject: [PATCH] dtm --- pkg/common/db/mysql.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/common/db/mysql.go b/pkg/common/db/mysql.go index d8370c99a..42200b40c 100644 --- a/pkg/common/db/mysql.go +++ b/pkg/common/db/mysql.go @@ -2,6 +2,7 @@ package db import ( "Open_IM/pkg/common/config" + "gorm.io/gorm/logger" "fmt" "sync" @@ -16,6 +17,12 @@ type mysqlDB struct { db *gorm.DB } +type Writer struct{} + +func (w Writer) Printf(format string, args ...interface{}) { + fmt.Printf(format, args...) +} + func initMysqlDB() { //When there is no open IM database, connect to the mysql built-in database to create openIM database dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", @@ -45,7 +52,19 @@ func initMysqlDB() { dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName) - db, err = gorm.Open(mysql.Open(dsn), nil) + + newLogger := logger.New( + Writer{}, + logger.Config{ + SlowThreshold: 200 * time.Millisecond, // Slow SQL threshold + LogLevel: logger.Info, // Log level + IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger + Colorful: true, // Disable color + }, + ) + db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{ + Logger: newLogger, + }) if err != nil { fmt.Println("0", "Open failed ", err.Error(), dsn) panic(err.Error())