caddyhttp: Split up logged remote address into IP and port (#4403)

This commit is contained in:
Francis Lavoie
2021-11-29 01:18:35 -05:00
committed by GitHub
parent 0eb0b60f47
commit f55b123d63
5 changed files with 22 additions and 8 deletions

View File

@ -16,6 +16,7 @@ package caddyhttp
import (
"crypto/tls"
"net"
"net/http"
"strings"
@ -27,7 +28,14 @@ type LoggableHTTPRequest struct{ *http.Request }
// MarshalLogObject satisfies the zapcore.ObjectMarshaler interface.
func (r LoggableHTTPRequest) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("remote_addr", r.RemoteAddr)
ip, port, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
ip = r.RemoteAddr
port = ""
}
enc.AddString("remote_ip", ip)
enc.AddString("remote_port", port)
enc.AddString("proto", r.Proto)
enc.AddString("method", r.Method)
enc.AddString("host", r.Host)