Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bscp支持配置gorm日志级别 #3489

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bcs-services/bcs-bscp/cmd/cache-service/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (cs *cacheService) prepare(opt *options.Option) error {
cs.bds = bds

// initial DAO set
set, err := dao.NewDaoSet(cc.CacheService().Sharding, cc.CacheService().Credential)
set, err := dao.NewDaoSet(cc.CacheService().Sharding, cc.CacheService().Credential, cc.CacheService().Gorm)
if err != nil {
return fmt.Errorf("initial dao set failed, err: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion bcs-services/bcs-bscp/cmd/data-service/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (ds *dataService) prepare(opt *options.Option) error {
}

// initial DAO set
set, err := dao.NewDaoSet(cc.DataService().Sharding, cc.DataService().Credential)
set, err := dao.NewDaoSet(cc.DataService().Sharding, cc.DataService().Credential, cc.DataService().Gorm)
if err != nil {
return fmt.Errorf("initial dao set failed, err: %v", err)
}
Expand Down
5 changes: 5 additions & 0 deletions bcs-services/bcs-bscp/etc/bcs-bscp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ redisCluster:
password:
db: 1

# gorm related settings
gorm:
# 日志级别,支持silent、error、warn、info四种级别(不区分大小写),默认为info
logLevel:

downstream:
bounceIntervalHour: 48

Expand Down
16 changes: 14 additions & 2 deletions bcs-services/bcs-bscp/pkg/cc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ type CacheServiceSetting struct {
Credential Credential `yaml:"credential"`
Sharding Sharding `yaml:"sharding"`
RedisCluster RedisCluster `yaml:"redisCluster"`
Gorm Gorm `yaml:"gorm"`
}

// trySetFlagBindIP try set flag bind ip.
Expand All @@ -215,6 +216,7 @@ func (s *CacheServiceSetting) trySetDefault() {
s.Log.trySetDefault()
s.Sharding.trySetDefault()
s.RedisCluster.trySetDefault()
s.Gorm.trySetDefault()
}

// Validate CacheServiceSetting option.
Expand All @@ -236,6 +238,10 @@ func (s CacheServiceSetting) Validate() error {
return err
}

if err := s.Gorm.validate(); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -300,6 +306,7 @@ type DataServiceSetting struct {
Repo Repository `yaml:"repository"`
Vault Vault `yaml:"vault"`
FeatureFlags FeatureFlags `yaml:"featureFlags"`
Gorm Gorm `yaml:"gorm"`
}

// trySetFlagBindIP try set flag bind ip.
Expand All @@ -321,6 +328,7 @@ func (s *DataServiceSetting) trySetDefault() {
s.Repo.trySetDefault()
s.Vault.getConfigFromEnv()
s.FeatureFlags.trySetDefault()
s.Gorm.trySetDefault()
}

// Validate DataServiceSetting option.
Expand Down Expand Up @@ -354,6 +362,10 @@ func (s DataServiceSetting) Validate() error {
return err
}

if err := s.Gorm.validate(); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -535,15 +547,15 @@ func (s *VaultServerSetting) trySetFlagPort(port, grpcPort int) error {
return s.Network.trySetFlagPort(port, grpcPort)
}

// trySetDefault set the CacheServiceSetting default value if user not configured.
// trySetDefault set the VaultServerSetting default value if user not configured.
func (s *VaultServerSetting) trySetDefault() {
s.Network.trySetDefault()
s.Service.trySetDefault()
s.Log.trySetDefault()
s.Sharding.trySetDefault()
}

// Validate CacheServiceSetting option.
// Validate VaultServerSetting option.
func (s VaultServerSetting) Validate() error {

if err := s.Network.validate(); err != nil {
Expand Down
56 changes: 56 additions & 0 deletions bcs-services/bcs-bscp/pkg/cc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

etcd3 "go.etcd.io/etcd/client/v3"
"gorm.io/gorm/logger"

"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/logs"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/tools"
Expand Down Expand Up @@ -1388,3 +1389,58 @@ func (g GSE) validate() error {
}
return nil
}

// Gorm defines the grom related settings.
type Gorm struct {
LogLevel GormLogLevel `yaml:"logLevel"`
}

// GormLogLevel is gorm log level type
type GormLogLevel string

const (
// GormLogSilent used for gorm log level
GormLogSilent GormLogLevel = "silent"
// GormLogError used for gorm log level
GormLogError GormLogLevel = "error"
// GormLogWarn used for gorm log level
GormLogWarn GormLogLevel = "warn"
// GormLogInfo used for gorm log level
GormLogInfo GormLogLevel = "info"
)

// GetLogLevel get log level for gorm.
func (g Gorm) GetLogLevel() logger.LogLevel {
switch strings.ToLower(string(g.LogLevel)) {
case string(GormLogSilent):
return logger.Silent
case string(GormLogError):
return logger.Error
case string(GormLogWarn):
return logger.Warn
case string(GormLogInfo):
return logger.Info
default:
return logger.Info
}
}

// validate if the gorm is valid or not.
func (g Gorm) validate() error {
switch strings.ToLower(string(g.LogLevel)) {
case string(GormLogSilent):
case string(GormLogError):
case string(GormLogWarn):
case string(GormLogInfo):
default:
return fmt.Errorf("unsopported log level: %s", g.LogLevel)
}
return nil
}

// trySetDefault try set the default value of gorm
func (g *Gorm) trySetDefault() {
if g.LogLevel == "" {
g.LogLevel = GormLogInfo
}
}
4 changes: 2 additions & 2 deletions bcs-services/bcs-bscp/pkg/dal/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type Set interface {
}

// NewDaoSet create the DAO set instance.
func NewDaoSet(opt cc.Sharding, credentialSetting cc.Credential) (Set, error) {
func NewDaoSet(opt cc.Sharding, credentialSetting cc.Credential, gormSetting cc.Gorm) (Set, error) {

// opt cc.Database
sd, err := sharding.InitSharding(&opt)
Expand All @@ -82,7 +82,7 @@ func NewDaoSet(opt cc.Sharding, credentialSetting cc.Credential) (Set, error) {
}

adminDB, err := gorm.Open(mysql.Open(sharding.URI(opt.AdminDatabase)),
&gorm.Config{Logger: logger.Default.LogMode(logger.Info)})
&gorm.Config{Logger: logger.Default.LogMode(gormSetting.GetLogLevel())})
if err != nil {
return nil, err
}
Expand Down
Loading