add clientIP for ws logger #51

This commit is contained in:
ginuerzh
2025-06-28 18:02:21 +08:00
parent 3ec2f9d487
commit fe096d6e74
+7 -1
View File
@@ -158,10 +158,16 @@ func (l *wsListener) Addr() net.Addr {
} }
func (l *wsListener) upgrade(w http.ResponseWriter, r *http.Request) { func (l *wsListener) upgrade(w http.ResponseWriter, r *http.Request) {
clientIP := xhttp.GetClientIP(r)
if l.logger.IsLevelEnabled(logger.TraceLevel) { if l.logger.IsLevelEnabled(logger.TraceLevel) {
sip := ""
if clientIP != nil {
sip = clientIP.String()
}
log := l.logger.WithFields(map[string]any{ log := l.logger.WithFields(map[string]any{
"local": l.addr.String(), "local": l.addr.String(),
"remote": r.RemoteAddr, "remote": r.RemoteAddr,
"clientIP": sip,
}) })
dump, _ := httputil.DumpRequest(r, false) dump, _ := httputil.DumpRequest(r, false)
log.Trace(string(dump)) log.Trace(string(dump))
@@ -175,7 +181,7 @@ func (l *wsListener) upgrade(w http.ResponseWriter, r *http.Request) {
} }
var clientAddr net.Addr var clientAddr net.Addr
if clientIP := xhttp.GetClientIP(r); clientIP != nil { if clientIP != nil {
clientAddr = &net.IPAddr{IP: clientIP} clientAddr = &net.IPAddr{IP: clientIP}
} }