refactor: improve logging for channel operations with detailed context

This commit is contained in:
CaIon
2025-08-15 14:15:03 +08:00
parent cc4f73dc7e
commit 1d4850e47a

View File

@@ -209,7 +209,7 @@ func (channel *Channel) GetOtherInfo() map[string]interface{} {
if channel.OtherInfo != "" { if channel.OtherInfo != "" {
err := common.Unmarshal([]byte(channel.OtherInfo), &otherInfo) err := common.Unmarshal([]byte(channel.OtherInfo), &otherInfo)
if err != nil { if err != nil {
common.SysLog("failed to unmarshal other info: " + err.Error()) common.SysLog(fmt.Sprintf("failed to unmarshal other info: channel_id=%d, tag=%s, name=%s, error=%v", channel.Id, channel.GetTag(), channel.Name, err))
} }
} }
return otherInfo return otherInfo
@@ -218,7 +218,7 @@ func (channel *Channel) GetOtherInfo() map[string]interface{} {
func (channel *Channel) SetOtherInfo(otherInfo map[string]interface{}) { func (channel *Channel) SetOtherInfo(otherInfo map[string]interface{}) {
otherInfoBytes, err := json.Marshal(otherInfo) otherInfoBytes, err := json.Marshal(otherInfo)
if err != nil { if err != nil {
common.SysLog("failed to marshal other info: " + err.Error()) common.SysLog(fmt.Sprintf("failed to marshal other info: channel_id=%d, tag=%s, name=%s, error=%v", channel.Id, channel.GetTag(), channel.Name, err))
return return
} }
channel.OtherInfo = string(otherInfoBytes) channel.OtherInfo = string(otherInfoBytes)
@@ -492,7 +492,7 @@ func (channel *Channel) UpdateResponseTime(responseTime int64) {
ResponseTime: int(responseTime), ResponseTime: int(responseTime),
}).Error }).Error
if err != nil { if err != nil {
common.SysLog("failed to update response time: " + err.Error()) common.SysLog(fmt.Sprintf("failed to update response time: channel_id=%d, error=%v", channel.Id, err))
} }
} }
@@ -502,7 +502,7 @@ func (channel *Channel) UpdateBalance(balance float64) {
Balance: balance, Balance: balance,
}).Error }).Error
if err != nil { if err != nil {
common.SysLog("failed to update balance: " + err.Error()) common.SysLog(fmt.Sprintf("failed to update balance: channel_id=%d, error=%v", channel.Id, err))
} }
} }
@@ -618,7 +618,7 @@ func UpdateChannelStatus(channelId int, usingKey string, status int, reason stri
if shouldUpdateAbilities { if shouldUpdateAbilities {
err := UpdateAbilityStatus(channelId, status == common.ChannelStatusEnabled) err := UpdateAbilityStatus(channelId, status == common.ChannelStatusEnabled)
if err != nil { if err != nil {
common.SysLog("failed to update ability status: " + err.Error()) common.SysLog(fmt.Sprintf("failed to update ability status: channel_id=%d, error=%v", channelId, err))
} }
} }
}() }()
@@ -646,7 +646,7 @@ func UpdateChannelStatus(channelId int, usingKey string, status int, reason stri
} }
err = channel.Save() err = channel.Save()
if err != nil { if err != nil {
common.SysLog("failed to update channel status: " + err.Error()) common.SysLog(fmt.Sprintf("failed to update channel status: channel_id=%d, status=%d, error=%v", channel.Id, status, err))
return false return false
} }
} }
@@ -708,7 +708,7 @@ func EditChannelByTag(tag string, newTag *string, modelMapping *string, models *
for _, channel := range channels { for _, channel := range channels {
err = channel.UpdateAbilities(nil) err = channel.UpdateAbilities(nil)
if err != nil { if err != nil {
common.SysLog("failed to update abilities: " + err.Error()) common.SysLog(fmt.Sprintf("failed to update abilities: channel_id=%d, tag=%s, error=%v", channel.Id, channel.GetTag(), err))
} }
} }
} }
@@ -732,7 +732,7 @@ func UpdateChannelUsedQuota(id int, quota int) {
func updateChannelUsedQuota(id int, quota int) { func updateChannelUsedQuota(id int, quota int) {
err := DB.Model(&Channel{}).Where("id = ?", id).Update("used_quota", gorm.Expr("used_quota + ?", quota)).Error err := DB.Model(&Channel{}).Where("id = ?", id).Update("used_quota", gorm.Expr("used_quota + ?", quota)).Error
if err != nil { if err != nil {
common.SysLog("failed to update channel used quota: " + err.Error()) common.SysLog(fmt.Sprintf("failed to update channel used quota: channel_id=%d, delta_quota=%d, error=%v", id, quota, err))
} }
} }
@@ -825,7 +825,7 @@ func (channel *Channel) GetSetting() dto.ChannelSettings {
if channel.Setting != nil && *channel.Setting != "" { if channel.Setting != nil && *channel.Setting != "" {
err := common.Unmarshal([]byte(*channel.Setting), &setting) err := common.Unmarshal([]byte(*channel.Setting), &setting)
if err != nil { if err != nil {
common.SysLog("failed to unmarshal setting: " + err.Error()) common.SysLog(fmt.Sprintf("failed to unmarshal setting: channel_id=%d, error=%v", channel.Id, err))
channel.Setting = nil // 清空设置以避免后续错误 channel.Setting = nil // 清空设置以避免后续错误
_ = channel.Save() // 保存修改 _ = channel.Save() // 保存修改
} }
@@ -836,7 +836,7 @@ func (channel *Channel) GetSetting() dto.ChannelSettings {
func (channel *Channel) SetSetting(setting dto.ChannelSettings) { func (channel *Channel) SetSetting(setting dto.ChannelSettings) {
settingBytes, err := common.Marshal(setting) settingBytes, err := common.Marshal(setting)
if err != nil { if err != nil {
common.SysLog("failed to marshal setting: " + err.Error()) common.SysLog(fmt.Sprintf("failed to marshal setting: channel_id=%d, error=%v", channel.Id, err))
return return
} }
channel.Setting = common.GetPointer[string](string(settingBytes)) channel.Setting = common.GetPointer[string](string(settingBytes))
@@ -847,7 +847,7 @@ func (channel *Channel) GetOtherSettings() dto.ChannelOtherSettings {
if channel.OtherSettings != "" { if channel.OtherSettings != "" {
err := common.UnmarshalJsonStr(channel.OtherSettings, &setting) err := common.UnmarshalJsonStr(channel.OtherSettings, &setting)
if err != nil { if err != nil {
common.SysLog("failed to unmarshal setting: " + err.Error()) common.SysLog(fmt.Sprintf("failed to unmarshal setting: channel_id=%d, error=%v", channel.Id, err))
channel.OtherSettings = "{}" // 清空设置以避免后续错误 channel.OtherSettings = "{}" // 清空设置以避免后续错误
_ = channel.Save() // 保存修改 _ = channel.Save() // 保存修改
} }
@@ -858,7 +858,7 @@ func (channel *Channel) GetOtherSettings() dto.ChannelOtherSettings {
func (channel *Channel) SetOtherSettings(setting dto.ChannelOtherSettings) { func (channel *Channel) SetOtherSettings(setting dto.ChannelOtherSettings) {
settingBytes, err := common.Marshal(setting) settingBytes, err := common.Marshal(setting)
if err != nil { if err != nil {
common.SysLog("failed to marshal setting: " + err.Error()) common.SysLog(fmt.Sprintf("failed to marshal setting: channel_id=%d, error=%v", channel.Id, err))
return return
} }
channel.OtherSettings = string(settingBytes) channel.OtherSettings = string(settingBytes)
@@ -869,7 +869,7 @@ func (channel *Channel) GetParamOverride() map[string]interface{} {
if channel.ParamOverride != nil && *channel.ParamOverride != "" { if channel.ParamOverride != nil && *channel.ParamOverride != "" {
err := common.Unmarshal([]byte(*channel.ParamOverride), &paramOverride) err := common.Unmarshal([]byte(*channel.ParamOverride), &paramOverride)
if err != nil { if err != nil {
common.SysLog("failed to unmarshal param override: " + err.Error()) common.SysLog(fmt.Sprintf("failed to unmarshal param override: channel_id=%d, error=%v", channel.Id, err))
} }
} }
return paramOverride return paramOverride