Skip to content

Commit

Permalink
fix: fortune bug 完善db语句日志打印
Browse files Browse the repository at this point in the history
  • Loading branch information
Azusa-Yuan committed Oct 3, 2024
1 parent 61ed7f4 commit 4cafce0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
25 changes: 13 additions & 12 deletions plugin/fortune/fortune.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (
type fortune struct {
GroupID int64 `gorm:"column:gid; uniqueIndex:gid_bid"`
BotID int64 `gorm:"column:bid; uniqueIndex:gid_bid"`
Value string `gorm:"column:value; uniqueIndex:gid_bid"`
Value string `gorm:"column:value"`
}

func init() {
Expand All @@ -69,6 +69,17 @@ func init() {
index[s] = uint8(i)
}

data, err := os.ReadFile(omikujson)
if err != nil {
log.Error().Err(err).Str("name", pluginName).Msg("")
return
}
err = json.Unmarshal(data, &omikujis)
if err != nil {
log.Error().Err(err).Str("name", pluginName).Msg("")
return
}

en.OnRegex(`^设置底图\s?(.*)`).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
gid := ctx.Event.GroupID
Expand All @@ -84,7 +95,7 @@ func init() {
Value: ctx.State["regex_matched"].([]string)[1],
}).Error
if err != nil {
ctx.SendChain(message.Text("设置失败:", err))
ctx.SendError(err)
return
}
ctx.SendChain(message.Text("设置成功~"))
Expand All @@ -93,16 +104,6 @@ func init() {
ctx.SendChain(message.Text("没有这个底图哦~"))
})

data, err := os.ReadFile(omikujson)
if err != nil {
log.Error().Err(err).Str("name", pluginName).Msg("")
return
}
err = json.Unmarshal(data, &omikujis)
if err != nil {
log.Error().Err(err).Str("name", pluginName).Msg("")
return
}
en.OnFullMatchGroup([]string{"运势", "抽签"}).SetBlock(true).
Handle(func(ctx *zero.Ctx) {

Expand Down
8 changes: 6 additions & 2 deletions utils/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ func init() {
if err != nil {
log.Error().Str("name", utilsName).Err(err).Msg("")
}
logLevel := logger.Silent
if log_utils.LogLevel == "debug" {
logLevel = logger.Info
}
gormLogger := &log_utils.GormLogger{Logger: log.Logger}
for k, v := range config.DbMap {
// ok2代表是否有该数据库,ok1代表是否开启该数据库,实际上不用的话,可以直接将数据库删除
if ok1, ok2 := config.DbType[v.Type]; ok2 && ok1 {
switch {
case v.Type == "pgsql":
v.Db, err = gorm.Open(postgres.Open(v.Dsn), &gorm.Config{
Logger: gormLogger.LogMode(logger.Silent),
Logger: gormLogger.LogMode(logLevel),
})

if err != nil {
Expand All @@ -73,7 +77,7 @@ func init() {
log.Info().Str("name", utilsName).Msgf("success open database %v", k)
case v.Type == "mysql":
v.Db, err = gorm.Open(mysql.Open(v.Dsn), &gorm.Config{
Logger: gormLogger.LogMode(logger.Error),
Logger: gormLogger.LogMode(logLevel),
})
if err != nil {
log.Error().Str("name", utilsName).Err(err).Msgf("Error open database %v", k)
Expand Down
4 changes: 3 additions & 1 deletion utils/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
)

var logSourceDir string
var LogLevel string

func init() {
if os.Getenv("LogLevel") == "debug" {
LogLevel = os.Getenv("LogLevel")
if LogLevel == "debug" {
log.Info().Msg("目前处于debug等级,请注意打印日志的等级")
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
Expand Down

0 comments on commit 4cafce0

Please sign in to comment.