refactor: extract proxyDebugLog helper for repeated debug logging

This commit is contained in:
xfy 2026-06-03 13:57:55 +08:00
parent 684122dbf7
commit 1ce42c039b

View File

@ -55,6 +55,29 @@ import (
"rua.plus/lolly/internal/variable" "rua.plus/lolly/internal/variable"
) )
// proxyDebugLog 在 DEBUG 级别记录代理日志
func proxyDebugLog(msg string, kv ...interface{}) {
if !logging.Debug().Enabled() {
return
}
event := logging.Debug()
for i := 0; i < len(kv)-1; i += 2 {
key, ok := kv[i].(string)
if !ok {
continue
}
switch v := kv[i+1].(type) {
case string:
event = event.Str(key, v)
case int:
event = event.Int(key, v)
case bool:
event = event.Bool(key, v)
}
}
event.Msg(msg)
}
const ( const (
// upstreamCache 上游缓存标识。 // upstreamCache 上游缓存标识。
// 用于标记请求可直接使用缓存响应,无需转发到上游。 // 用于标记请求可直接使用缓存响应,无需转发到上游。
@ -466,14 +489,11 @@ func FinalizeUpstreamVars(vc *variable.Context, upstreamAddr string, upstreamSta
// 如果没有可用的健康目标,返回 502 Bad Gateway。 // 如果没有可用的健康目标,返回 502 Bad Gateway。
// 如果后端请求失败,根据 next_upstream 配置尝试下一个目标。 // 如果后端请求失败,根据 next_upstream 配置尝试下一个目标。
func (p *Proxy) ServeHTTP(ctx *fasthttp.RequestCtx) { func (p *Proxy) ServeHTTP(ctx *fasthttp.RequestCtx) {
// DEBUG: 打印请求信息(条件化避免非 Debug 级别的 string() 分配) proxyDebugLog("[PROXY] 收到请求",
if logging.Debug().Enabled() { "path", b2s(ctx.Path()),
logging.Debug(). "host", b2s(ctx.Host()),
Str("path", b2s(ctx.Path())). "method", b2s(ctx.Method()),
Str("host", b2s(ctx.Host())). )
Str("method", b2s(ctx.Method())).
Msg("[PROXY] 收到请求")
}
// 上游变量捕获 // 上游变量捕获
var upstreamAddr string var upstreamAddr string
@ -532,13 +552,10 @@ func (p *Proxy) ServeHTTP(ctx *fasthttp.RequestCtx) {
attemptedTargets = append(attemptedTargets, target) attemptedTargets = append(attemptedTargets, target)
// DEBUG: 打印选中的目标(条件化避免分配) proxyDebugLog("[PROXY] 选中目标",
if logging.Debug().Enabled() { "url", target.URL,
logging.Debug(). "healthy", target.Healthy.Load(),
Str("url", target.URL). )
Bool("healthy", target.Healthy.Load()).
Msg("[PROXY] 选中目标")
}
// 获取所选目标的客户端 // 获取所选目标的客户端
client := p.getClient(target.URL) client := p.getClient(target.URL)
@ -551,13 +568,10 @@ func (p *Proxy) ServeHTTP(ctx *fasthttp.RequestCtx) {
continue continue
} }
// DEBUG: 打印客户端信息(条件化避免分配) proxyDebugLog("[PROXY] client 信息",
if logging.Debug().Enabled() { "addr", client.Addr,
logging.Debug(). "isTLS", client.IsTLS,
Str("addr", client.Addr). )
Bool("isTLS", client.IsTLS).
Msg("[PROXY] client 信息")
}
// 增加连接计数(用于最少连接数负载均衡) // 增加连接计数(用于最少连接数负载均衡)
loadbalance.IncrementConnections(target) loadbalance.IncrementConnections(target)
@ -623,14 +637,11 @@ func (p *Proxy) ServeHTTP(ctx *fasthttp.RequestCtx) {
} }
req.SetRequestURIBytes(targetURI) req.SetRequestURIBytes(targetURI)
// DEBUG: 打印请求头(条件化避免分配) proxyDebugLog("[PROXY] 请求准备完成",
if logging.Debug().Enabled() { "host", b2s(req.Header.Host()),
logging.Debug(). "uri", b2s(req.RequestURI()),
Str("host", b2s(req.Header.Host())). "targetURI", b2s(targetURI),
Str("uri", b2s(req.RequestURI())). )
Str("targetURI", b2s(targetURI)).
Msg("[PROXY] 请求准备完成")
}
// 尝试从缓存获取(如果启用) // 尝试从缓存获取(如果启用)
if p.cache != nil && attempt == 0 { if p.cache != nil && attempt == 0 {
@ -712,12 +723,10 @@ func (p *Proxy) ServeHTTP(ctx *fasthttp.RequestCtx) {
if err != nil { if err != nil {
logging.Error().Msgf("[PROXY] 请求失败: url=%s, err=%v, errType=%T", target.URL, err, err) logging.Error().Msgf("[PROXY] 请求失败: url=%s, err=%v, errType=%T", target.URL, err, err)
} else { } else {
if logging.Debug().Enabled() { proxyDebugLog("[PROXY] 请求成功",
logging.Debug(). "url", target.URL,
Str("url", target.URL). "status", ctx.Response.StatusCode(),
Int("status", ctx.Response.StatusCode()). )
Msg("[PROXY] 请求成功")
}
} }
if err != nil { if err != nil {