@@ -149,9 +149,9 @@ func (p *Proxy) Forward(w http.ResponseWriter, r *http.Request) {
149149
150150 // 处理响应
151151 if resp .StatusCode == http .StatusOK && isEventStream (resp .Header .Get ("Content-Type" )) {
152- p .handleStreamResponseWithStats (w , resp , startTime , r .Method , r .URL .Path , model , clientIP , inputTokens , string (body ))
152+ p .handleStreamResponseWithStats (w , resp , startTime , r .Method , r .URL .Path , targetURL , model , clientIP , inputTokens , string (body ))
153153 } else {
154- p .handleNormalResponseWithStats (w , resp , startTime , r .Method , r .URL .Path , model , clientIP , inputTokens , string (body ))
154+ p .handleNormalResponseWithStats (w , resp , startTime , r .Method , r .URL .Path , targetURL , model , clientIP , inputTokens , string (body ))
155155 }
156156}
157157
@@ -215,7 +215,7 @@ func (p *Proxy) buildHeaders(provider *config.ProviderConfig, apiKey string, req
215215}
216216
217217// handleStreamResponseWithStats 处理流式响应并统计
218- func (p * Proxy ) handleStreamResponseWithStats (w http.ResponseWriter , resp * http.Response , startTime time.Time , method , path , model , clientIP string , inputTokens int , requestBody string ) {
218+ func (p * Proxy ) handleStreamResponseWithStats (w http.ResponseWriter , resp * http.Response , startTime time.Time , method , path , targetURL , model , clientIP string , inputTokens int , requestBody string ) {
219219 copyHeaders (w .Header (), resp .Header )
220220
221221 // 设置 SSE 头
@@ -289,7 +289,8 @@ func (p *Proxy) handleStreamResponseWithStats(w http.ResponseWriter, resp *http.
289289 duration := time .Since (startTime ).Milliseconds ()
290290 totalTokens := inputTokens + outputTokens
291291
292- p .logForwardResponse (model , outputTokens )
292+ // 打印响应日志
293+ p .logResponse (method , path , targetURL , resp .StatusCode , duration , clientIP , responseBuf .String ())
293294
294295 // 保存记录
295296 record := & storage.RequestRecord {
@@ -314,7 +315,7 @@ func (p *Proxy) handleStreamResponseWithStats(w http.ResponseWriter, resp *http.
314315}
315316
316317// handleNormalResponseWithStats 处理普通响应并统计
317- func (p * Proxy ) handleNormalResponseWithStats (w http.ResponseWriter , resp * http.Response , startTime time.Time , method , path , model , clientIP string , inputTokens int , requestBody string ) {
318+ func (p * Proxy ) handleNormalResponseWithStats (w http.ResponseWriter , resp * http.Response , startTime time.Time , method , path , targetURL , model , clientIP string , inputTokens int , requestBody string ) {
318319 // 读取响应体
319320 respBody , err := io .ReadAll (resp .Body )
320321 if err != nil {
@@ -350,7 +351,8 @@ func (p *Proxy) handleNormalResponseWithStats(w http.ResponseWriter, resp *http.
350351
351352 totalTokens := inputTokens + outputTokens
352353
353- p .logForwardResponse (model , outputTokens )
354+ // 打印响应日志
355+ p .logResponse (method , path , targetURL , resp .StatusCode , duration , clientIP , string (respBody ))
354356
355357 // 保存记录
356358 record := & storage.RequestRecord {
@@ -510,6 +512,33 @@ func (p *Proxy) logForwardResponse(model string, outputTokens int) {
510512 fmt .Fprintf (p .logOutput (), "时间:%s 转发响应:模型:%s token数:%d\n " , humanLogTime (), displayModel (model ), outputTokens )
511513}
512514
515+ // logResponse 打印响应日志
516+ func (p * Proxy ) logResponse (method , path , targetURL string , statusCode int , duration int64 , clientIP , responseBody string ) {
517+ // 判断是否是错误状态码 (4xx 或 5xx)
518+ isError := statusCode >= 400 && statusCode < 600
519+
520+ fields := []zap.Field {
521+ zap .String ("method" , method ),
522+ zap .String ("path" , path ),
523+ zap .String ("target" , targetURL ),
524+ zap .Int ("status" , statusCode ),
525+ zap .Int64 ("duration_ms" , duration ),
526+ zap .String ("remote" , clientIP ),
527+ }
528+
529+ if isError {
530+ // 限制响应体长度,避免日志过大
531+ truncatedBody := responseBody
532+ if len (truncatedBody ) > 500 {
533+ truncatedBody = truncatedBody [:500 ] + "...(truncated)"
534+ }
535+ fields = append (fields , zap .String ("response" , truncatedBody ))
536+ p .logger .Warn ("代理响应" , fields ... )
537+ } else {
538+ p .logger .Info ("代理响应" , fields ... )
539+ }
540+ }
541+
513542func (p * Proxy ) logOutput () io.Writer {
514543 if p .output != nil {
515544 return p .output
0 commit comments