feat: 错误日志记录

This commit is contained in:
tbphp
2025-06-10 11:09:13 +08:00
parent 6b33f7054d
commit 272d36add5

View File

@@ -4,6 +4,7 @@ package proxy
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
@@ -148,7 +149,7 @@ func (ps *ProxyServer) executeRequestWithRetry(c *gin.Context, startTime time.Ti
// Check retry limit
if retryCount >= keysConfig.MaxRetries {
logrus.Errorf("Max retries exceeded (%d)", retryCount)
logrus.Debugf("Max retries exceeded (%d)", retryCount)
// Return detailed error information
errorResponse := gin.H{
@@ -288,8 +289,7 @@ func (ps *ProxyServer) executeRequestWithRetry(c *gin.Context, startTime time.Ti
responseTime := time.Since(startTime)
// Check if HTTP status code requires retry
// 429 (Too Many Requests) and 5xx server errors need retry
if resp.StatusCode == 429 || resp.StatusCode >= 500 {
if resp.StatusCode >= 400 {
// Log failure
if retryCount > 0 {
logrus.Debugf("Retry request returned error %d (attempt %d) (response time: %v)", resp.StatusCode, retryCount+1, responseTime)
@@ -305,6 +305,18 @@ func (ps *ProxyServer) executeRequestWithRetry(c *gin.Context, startTime time.Ti
errorMessage = fmt.Sprintf("HTTP %d", resp.StatusCode)
}
var jsonError struct {
Error struct {
Message string `json:"message"`
} `json:"error"`
}
if err := json.Unmarshal([]byte(errorMessage), &jsonError); err == nil && jsonError.Error.Message != "" {
logrus.Errorf("Error message: %s", jsonError.Error.Message)
} else {
logrus.Errorf("Error message: %s", errorMessage)
}
// Record failure asynchronously
go ps.keyManager.RecordFailure(keyInfo.Key, fmt.Errorf("HTTP %d", resp.StatusCode))
@@ -329,7 +341,7 @@ func (ps *ProxyServer) executeRequestWithRetry(c *gin.Context, startTime time.Ti
// Log final success result
if retryCount > 0 {
logrus.Infof("Request succeeded after %d retries (response time: %v)", retryCount, responseTime)
logrus.Debugf("Request succeeded after %d retries (response time: %v)", retryCount, responseTime)
} else {
logrus.Debugf("Request succeeded on first attempt (response time: %v)", responseTime)
}