Browse Source

fix: 替换 println 为 SysLog,修复 logger nil context 崩溃

- logger: 增加 ctx nil 检查,避免系统级日志 panic
- relay: 将 DebugEnabled 下的 println 替换为 SysLog
- price: 修复渠道定价在 ChannelMeta 未初始化时的获取失败问题

Co-Authored-By: Claude <noreply@anthropic.com>
master
fengsilin 1 week ago
parent
commit
a0ac37b2a9
4 changed files with 19 additions and 13 deletions
  1. +5
    -3
      logger/logger.go
  2. +2
    -2
      relay/channel/api_request.go
  3. +1
    -1
      relay/claude_handler.go
  4. +11
    -7
      relay/helper/price.go

+ 5
- 3
logger/logger.go View File

@@ -79,9 +79,11 @@ func logHelper(ctx context.Context, level string, msg string) {
if level == loggerINFO {
writer = gin.DefaultWriter
}
id := ctx.Value(common.RequestIdKey)
if id == nil {
id = "SYSTEM"
id := "SYSTEM"
if ctx != nil {
if v := ctx.Value(common.RequestIdKey); v != nil {
id = fmt.Sprintf("%v", v)
}
}
now := time.Now()
_, _ = fmt.Fprintf(writer, "[%s] %v | %s | %s \n", level, now.Format("2006/01/02 - 15:04:05"), id, msg)


+ 2
- 2
relay/channel/api_request.go View File

@@ -282,7 +282,7 @@ func DoApiRequest(a Adaptor, c *gin.Context, info *common.RelayInfo, requestBody
return nil, fmt.Errorf("get request url failed: %w", err)
}
if common2.DebugEnabled {
println("fullRequestURL:", fullRequestURL)
common2.SysLog("fullRequestURL: " + fullRequestURL)
}
req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody)
if err != nil {
@@ -313,7 +313,7 @@ func DoFormRequest(a Adaptor, c *gin.Context, info *common.RelayInfo, requestBod
return nil, fmt.Errorf("get request url failed: %w", err)
}
if common2.DebugEnabled {
println("fullRequestURL:", fullRequestURL)
common2.SysLog("fullRequestURL: " + fullRequestURL)
}
req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody)
if err != nil {


+ 1
- 1
relay/claude_handler.go View File

@@ -160,7 +160,7 @@ func ClaudeHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *typ
}

if common.DebugEnabled {
println("requestBody: ", string(jsonData))
common.SysLog("claude requestBody: " + string(jsonData))
}
requestBody = bytes.NewBuffer(jsonData)
}


+ 11
- 7
relay/helper/price.go View File

@@ -4,6 +4,7 @@ import (
"fmt"

"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/logger"
"github.com/QuantumNous/new-api/model"
relaycommon "github.com/QuantumNous/new-api/relay/common"
@@ -52,10 +53,16 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
var cacheRatio, cacheCreationRatio, imageRatio, audioRatio, audioCompletionRatio float64

// 尝试获取渠道定价(优先于全局定价)
channelMetaAvailable := info != nil && info.ChannelMeta != nil && info.ChannelId > 0
if channelMetaAvailable {
cp, found := model.GetEffectivePricing(info.OriginModelName, info.ChannelId)
if found {
// ChannelMeta 在 InitChannelMeta 之前为 nil,但 Distribute 已将 channelId 写入 context
channelId := 0
if info != nil && info.ChannelMeta != nil && info.ChannelId > 0 {
channelId = info.ChannelId
} else {
channelId = common.GetContextKeyInt(c, constant.ContextKeyChannelId)
}
if channelId > 0 {
cp, found := model.GetEffectivePricing(info.OriginModelName, channelId)
if found && cp != nil {
modelRatio = cp.ModelRatio
completionRatio = cp.CompletionRatio
modelPrice = cp.ModelPrice
@@ -67,9 +74,6 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
imageRatio = cp.ImageRatio
audioRatio = cp.AudioRatio
audioCompletionRatio = cp.AudioCompletionRatio
if common.DebugEnabled {
println(fmt.Sprintf("[ChannelPricing] hit: model=%s channel=%d source=cache", info.OriginModelName, info.ChannelId))
}
}
}



Loading…
Cancel
Save