Skip to content

Commit e709d26

Browse files
authored
feat: Configure GORM log level settings (#652)
* feat : gorm log level config * feat : gorm log level config
1 parent 1ae057c commit e709d26

7 files changed

Lines changed: 56 additions & 2 deletions

File tree

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ DB_CONN_MAX_LIFETIME=3600
132132
# DB_EXTRAS in GORM format
133133
DB_EXTRAS=
134134
DB_CHARSET=
135+
# The DB_GORM_LOG_LEVEL environment variable controls the logging verbosity of GORM
136+
# with a default value of empty string.
137+
# Possible values are "info", "warn", "error", and "silent".
138+
# "warn" is the default level, showing only error and warning logs
139+
# "info" level displays SQL statements
140+
# Refer to gorm.io/gorm/logger#LogLevel for detailed information.
141+
DB_GORM_LOG_LEVEL=
135142

136143
DIFY_INVOCATION_CONNECTION_IDLE_TIMEOUT=120
137144

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
go.opentelemetry.io/otel v1.41.0
2424
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.41.0
2525
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.41.0
26+
go.opentelemetry.io/otel/metric v1.41.0
2627
go.opentelemetry.io/otel/sdk v1.41.0
2728
go.opentelemetry.io/otel/sdk/metric v1.41.0
2829
go.opentelemetry.io/otel/trace v1.41.0
@@ -146,7 +147,6 @@ require (
146147
go.opentelemetry.io/contrib/detectors/gcp v1.39.0 // indirect
147148
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0 // indirect
148149
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.41.0 // indirect
149-
go.opentelemetry.io/otel/metric v1.41.0 // indirect
150150
go.opentelemetry.io/proto/otlp v1.9.0 // indirect
151151
golang.org/x/mod v0.32.0 // indirect
152152
golang.org/x/oauth2 v0.35.0 // indirect

internal/db/config/config.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package config
2+
3+
import (
4+
"gorm.io/gorm/logger"
5+
)
6+
7+
// GORM_LOG_LEVEL_MAP
8+
var gormLogLevelMap = map[string]logger.LogLevel{
9+
"silent": logger.Silent,
10+
"error": logger.Error,
11+
"warn": logger.Warn,
12+
"info": logger.Info,
13+
}
14+
15+
func ValidateGormLogLevel(level string) bool {
16+
_, ok := gormLogLevelMap[level]
17+
return ok
18+
}
19+
20+
func GetGormLogLevel(level string) logger.LogLevel {
21+
return gormLogLevelMap[level]
22+
}

internal/db/init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func Init(config *app.Config) {
8080
// enable prepared statements only for native PostgreSQL, disable for PgBouncer
8181
// as it's not supported on transaction pooling mode
8282
PreparedStatements: config.DBType == app.DB_TYPE_POSTGRESQL,
83+
LogLevel: config.DBGormLogLevel,
8384
})
8485
} else if config.DBType == app.DB_TYPE_MYSQL {
8586
DifyPluginDB, err = mysql.InitPluginDB(&mysql.MySQLConfig{
@@ -95,6 +96,7 @@ func Init(config *app.Config) {
9596
ConnMaxLifetime: config.DBConnMaxLifetime,
9697
Charset: config.DBCharset,
9798
Extras: config.DBExtras,
99+
LogLevel: config.DBGormLogLevel,
98100
})
99101
} else {
100102
log.Panic("unsupported database type", "type", config.DBType)

internal/db/mysql/mysql.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"fmt"
55
"time"
66

7+
gormConfig "github.com/langgenius/dify-plugin-daemon/internal/db/config"
78
"gorm.io/driver/mysql"
89
"gorm.io/gorm"
10+
"gorm.io/gorm/logger"
911
)
1012

1113
type MySQLConfig struct {
@@ -21,6 +23,7 @@ type MySQLConfig struct {
2123
ConnMaxLifetime int
2224
Charset string
2325
Extras string
26+
LogLevel string
2427
}
2528

2629
func InitPluginDB(config *MySQLConfig) (*gorm.DB, error) {
@@ -31,6 +34,7 @@ func InitPluginDB(config *MySQLConfig) (*gorm.DB, error) {
3134
user: config.User,
3235
password: config.Pass,
3336
sslMode: config.SSLMode,
37+
logLevel: config.LogLevel,
3438
}
3539

3640
// first try to connect to target database
@@ -74,11 +78,17 @@ type mysqlDbInitializer struct {
7478
user string
7579
password string
7680
sslMode string
81+
logLevel string
7782
}
7883

7984
func (m *mysqlDbInitializer) connect(dbName string) (*gorm.DB, error) {
8085
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=true&tls=%v", m.user, m.password, m.host, m.port, dbName, m.sslMode == "require")
81-
return gorm.Open(myDialector{Dialector: mysql.Open(dsn).(*mysql.Dialector)}, &gorm.Config{})
86+
87+
config := &gorm.Config{}
88+
if m.logLevel != "" {
89+
config.Logger = logger.Default.LogMode(gormConfig.GetGormLogLevel(m.logLevel))
90+
}
91+
return gorm.Open(myDialector{Dialector: mysql.Open(dsn).(*mysql.Dialector)}, config)
8292
}
8393

8494
func (m *mysqlDbInitializer) createDatabaseIfNotExists(db *gorm.DB, dbName string) error {

internal/db/pg/pg.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"strings"
66
"time"
77

8+
appDbConfig "github.com/langgenius/dify-plugin-daemon/internal/db/config"
89
"gorm.io/driver/postgres"
910
"gorm.io/gorm"
11+
"gorm.io/gorm/logger"
1012
)
1113

1214
type PGConfig struct {
@@ -23,6 +25,7 @@ type PGConfig struct {
2325
Charset string
2426
Extras string
2527
PreparedStatements bool
28+
LogLevel string
2629
}
2730

2831
func InitPluginDB(config *PGConfig) (*gorm.DB, error) {
@@ -31,6 +34,9 @@ func InitPluginDB(config *PGConfig) (*gorm.DB, error) {
3134
gormConfig := &gorm.Config{
3235
PrepareStmt: config.PreparedStatements,
3336
}
37+
if config.LogLevel != "" {
38+
gormConfig.Logger = logger.Default.LogMode(appDbConfig.GetGormLogLevel(config.LogLevel))
39+
}
3440
db, err := gorm.Open(postgres.Open(dsn), gormConfig)
3541
if err != nil {
3642
// if connection fails, try to create database

internal/types/app/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/go-playground/validator/v10"
11+
gormConfig "github.com/langgenius/dify-plugin-daemon/internal/db/config"
1112
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
1213
)
1314

@@ -151,6 +152,7 @@ type Config struct {
151152
DBConnMaxLifetime int `envconfig:"DB_CONN_MAX_LIFETIME" default:"3600"`
152153
DBExtras string `envconfig:"DB_EXTRAS"`
153154
DBCharset string `envconfig:"DB_CHARSET"`
155+
DBGormLogLevel string `envconfig:"DB_GORM_LOG_LEVEL"`
154156

155157
// persistence storage
156158
PersistenceStoragePath string `envconfig:"PERSISTENCE_STORAGE_PATH"`
@@ -291,6 +293,11 @@ func (c *Config) Validate() error {
291293
return fmt.Errorf("plugin package cache path is empty")
292294
}
293295

296+
if c.DBGormLogLevel != "" {
297+
if !gormConfig.ValidateGormLogLevel(c.DBGormLogLevel) {
298+
return fmt.Errorf("invalid gorm log level: '%s'. Valid levels are: silent, error, warn, info", c.DBGormLogLevel)
299+
}
300+
}
294301
return nil
295302
}
296303

0 commit comments

Comments
 (0)