Skip to content

Commit d3b069d

Browse files
committed
增加mysql的loglevel参数
1 parent 4188e23 commit d3b069d

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

config/config.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"embed"
66
"github.com/spf13/viper"
7+
"gorm.io/gorm/logger"
78
)
89

910
// ConfEnv env环境变量
@@ -37,15 +38,16 @@ type (
3738
DirPath string `yaml:"dirPath" default:"runtime/logs"`
3839
}
3940
Mysql []struct {
40-
Host string `yaml:"host" default:"127.0.0.1"`
41-
Port string `yaml:"port" default:"3306"`
42-
User string `yaml:"user" default:"root"`
43-
Password string `yaml:"password" default:"123456"`
44-
DbName string `yaml:"dbname"`
45-
Prefix string `yaml:"prefix" default:""`
46-
MaxIdleConns int `yaml:"maxIdleConns" default:"10"`
47-
MaxOpenConns int `yaml:"maxOpenConns" default:"100"`
48-
MaxLifeTime int `yaml:"maxLifeTime" default:"60"`
41+
Host string `yaml:"host" default:"127.0.0.1"`
42+
Port string `yaml:"port" default:"3306"`
43+
User string `yaml:"user" default:"root"`
44+
Password string `yaml:"password" default:"123456"`
45+
DbName string `yaml:"dbname"`
46+
Prefix string `yaml:"prefix" default:""`
47+
MaxIdleConns int `yaml:"maxIdleConns" default:"10"`
48+
MaxOpenConns int `yaml:"maxOpenConns" default:"100"`
49+
MaxLifeTime int `yaml:"maxLifeTime" default:"60"`
50+
LogLevel logger.LogLevel `yaml:"logLevel" default:"1"`
4951
}
5052
Redis struct {
5153
Host string `yaml:"host" default:"127.0.0.1"`

config/yaml/config.dev.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mysql:
2222
maxIdleConns: 10 # 设置空闲连接池中连接的最大数量
2323
maxOpenConns: 100 # 设置打开数据库连接的最大数量
2424
maxLifeTime: 60 # 设置了连接可复用的最大时间(分钟)
25+
logLevel: 4 # 日志模式 1:slient 2:error 3:warn 4:info
2526
redis:
2627
host: '127.0.0.1'
2728
port: '6379'

config/yaml/config.prod.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mysql:
2222
maxIdleConns: 10 # 设置空闲连接池中连接的最大数量
2323
maxOpenConns: 100 # 设置打开数据库连接的最大数量
2424
maxLifeTime: 60 # 设置了连接可复用的最大时间(分钟)
25+
logLevel: 1 # 日志模式 1:slient 2:error 3:warn 4:info
2526
redis:
2627
host: '127.0.0.1'
2728
port: '6379'

config/yaml/config.test.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mysql:
2222
maxIdleConns: 10 # 设置空闲连接池中连接的最大数量
2323
maxOpenConns: 100 # 设置打开数据库连接的最大数量
2424
maxLifeTime: 60 # 设置了连接可复用的最大时间(分钟)
25+
logLevel: 3 # 日志模式 1:slient 2:error 3:warn 4:info
2526
redis:
2627
host: '127.0.0.1'
2728
port: '6379'

pkg/lib/mysql.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package lib
22

33
import (
4+
"database/sql"
45
"fmt"
56
"gorm.io/driver/mysql"
67
"gorm.io/gorm"
8+
"gorm.io/gorm/logger"
79
"gorm.io/gorm/schema"
810
"time"
911
)
@@ -18,6 +20,7 @@ type DatabaseConfig struct {
1820
MaxIdleConns int
1921
MaxOpenConns int
2022
MaxLifeTime int // 分钟
23+
LogLevel logger.LogLevel
2124
}
2225

2326
// NewMysql 数据库连接
@@ -28,6 +31,7 @@ func NewMysql(config DatabaseConfig) (*gorm.DB, error) {
2831
TablePrefix: config.Prefix,
2932
SingularTable: true, // 是否设置单数表名,设置为 是
3033
},
34+
Logger: logger.Default.LogMode(config.LogLevel),
3135
})
3236
if err != nil {
3337
return nil, fmt.Errorf("Unable to connect to the database, please check the MySQL configuration information first,the error details are:" + err.Error())
@@ -42,3 +46,25 @@ func NewMysql(config DatabaseConfig) (*gorm.DB, error) {
4246
sqlDB.SetConnMaxLifetime(time.Duration(config.MaxLifeTime))
4347
return db, nil
4448
}
49+
50+
// NewNullString
51+
func NewNullString(s string) sql.NullString {
52+
if len(s) == 0 {
53+
return sql.NullString{}
54+
}
55+
return sql.NullString{
56+
String: s,
57+
Valid: true,
58+
}
59+
}
60+
61+
// NewNullInt64
62+
func NewNullInt64(s int64) sql.NullInt64 {
63+
if s == 0 {
64+
return sql.NullInt64{}
65+
}
66+
return sql.NullInt64{
67+
Int64: s,
68+
Valid: true,
69+
}
70+
}

0 commit comments

Comments
 (0)