Forráskód Böngészése

添加日志级别

yjp 1 éve
szülő
commit
a39d7bfb41

+ 23 - 1
framework/core/infrastructure/database/operations/db.go

@@ -8,6 +8,13 @@ import (
 	"gorm.io/gorm/logger"
 )
 
+const (
+	logLevelSilent = "silent"
+	logLevelError  = "error"
+	logLevelWarn   = "warn"
+	logLevelInfo   = "info"
+)
+
 func newGormDB(dbConfig *Config) (*gorm.DB, error) {
 	if dbConfig == nil {
 		return nil, errors.New("没有传递数据库配置")
@@ -25,7 +32,7 @@ func newPostgresGormDB(dbConfig *Config) (*gorm.DB, error) {
 	dsn := "host=%s port=%s user=%s password=%s dbname=%s sslmode=disable TimeZone=Asia/Shanghai"
 	connStr := fmt.Sprintf(dsn, dbConfig.Address, dbConfig.Port, dbConfig.UserName, dbConfig.Password, dbConfig.Database)
 	gormDB, err := gorm.Open(postgres.Open(connStr), &gorm.Config{
-		Logger:      logger.Default.LogMode(logger.Info),
+		Logger:      logger.Default.LogMode(chooseLogLevel(dbConfig.LogLevel)),
 		PrepareStmt: true,
 	})
 	if err != nil {
@@ -54,3 +61,18 @@ func destroyGormDB(gormDB *gorm.DB) error {
 
 	return nil
 }
+
+func chooseLogLevel(logLevel string) logger.LogLevel {
+	switch logLevel {
+	case logLevelSilent:
+		return logger.Silent
+	case logLevelError:
+		return logger.Error
+	case logLevelWarn:
+		return logger.Warn
+	case logLevelInfo:
+		return logger.Info
+	default:
+		return logger.Info
+	}
+}

+ 1 - 0
framework/core/infrastructure/database/operations/operations.go

@@ -19,6 +19,7 @@ type Config struct {
 	Database           string `json:"database" yaml:"database"`
 	MaxConnections     int    `json:"max_connections" yaml:"max_connections"`
 	MaxIdleConnections int    `json:"max_idle_connections" yaml:"max_idle_connections"`
+	LogLevel           string `json:"log_level" yaml:"log_level"`
 }
 
 type Operations struct {