diff --git a/config/fun.go b/config/fun.go index c037e68..fdc8dba 100644 --- a/config/fun.go +++ b/config/fun.go @@ -60,6 +60,7 @@ func SetDefault() { viper.Set("SERVER.mode", "release") viper.Set("DATABASE.type", "sqlite") viper.Set("DATABASE.dns", gfile.Join(gfile.Pwd(), "db", "database.db")) + viper.Set("DATABASE.debug", true) viper.Set("JWT.secret", "SET-YOUR-SECRET") viper.Set("JWT.expire", 86400) } diff --git a/config/structs.go b/config/structs.go index a2906d8..ff1aaad 100644 --- a/config/structs.go +++ b/config/structs.go @@ -15,8 +15,9 @@ type ServerConfig struct { // DataBaseConfig 数据库配置 type DataBaseConfig struct { - Dns string `mapstructure:"dns"` - Type string `mapstructure:"type"` + Dns string `mapstructure:"dns"` + Type string `mapstructure:"type"` + Debug bool `mapstructure:"debug"` // 是否开启 GORM 查询日志 } // JwtConfig JWT配置 diff --git a/database/database.go b/database/database.go index 5ce41e7..3a0e89c 100644 --- a/database/database.go +++ b/database/database.go @@ -11,6 +11,7 @@ import ( "github.com/gogf/gf/v2/os/gfile" "gorm.io/driver/mysql" "gorm.io/gorm" + "gorm.io/gorm/logger" "gorm.io/gorm/schema" ) @@ -24,24 +25,36 @@ var ( func init() { if g.IsEmpty(dns) { - log.Error("gormDns未配置", "请检查配置文件") + log.Error("gormDns 未配置", "请检查配置文件") return } switch config.GetConfigValue("database.type", "sqlite").String() { case "mysql": - log.Info("使用mysql数据库") + log.Info("使用 mysql 数据库") mysqlInit() case "sqlite": - log.Info("使用sqlite数据库") + log.Info("使用 sqlite 数据库") sqliteInit() } - Db, err = gorm.Open(Type, &gorm.Config{ + + // 构建 GORM 配置 + gormConfig := &gorm.Config{ SkipDefaultTransaction: true, // 命名策略:保持与模型一致,避免字段/表名转换问题 NamingStrategy: schema.NamingStrategy{ SingularTable: true, // 表名禁用复数形式(例如 User 对应 user 表,而非 users) }, - }) + } + + // 根据配置决定是否开启 GORM 查询日志 + if config.GetConfigValue("database.debug", false).Bool() { + log.Info("已开启 GORM 查询日志") + gormConfig.Logger = logger.Default.LogMode(logger.Info) + } else { + gormConfig.Logger = logger.Default.LogMode(logger.Silent) + } + + Db, err = gorm.Open(Type, gormConfig) if err != nil { log.Error("数据库连接失败: ", err) return