From f5344f8caddee53cebe249712ba7803e22abf9f1 Mon Sep 17 00:00:00 2001
From: Francis Lavoie <lavofr@gmail.com>
Date: Tue, 23 Jan 2024 19:45:50 -0500
Subject: [PATCH] caddyhttp: Fix panic when request missing ClientIPVarKey
 (#6040)

---
 modules/caddyhttp/marshalers.go | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/modules/caddyhttp/marshalers.go b/modules/caddyhttp/marshalers.go
index 9a955e3b6..8f4472a27 100644
--- a/modules/caddyhttp/marshalers.go
+++ b/modules/caddyhttp/marshalers.go
@@ -40,7 +40,9 @@ func (r LoggableHTTPRequest) MarshalLogObject(enc zapcore.ObjectEncoder) error {
 
 	enc.AddString("remote_ip", ip)
 	enc.AddString("remote_port", port)
-	enc.AddString("client_ip", GetVar(r.Context(), ClientIPVarKey).(string))
+	if ip, ok := GetVar(r.Context(), ClientIPVarKey).(string); ok {
+		enc.AddString("client_ip", ip)
+	}
 	enc.AddString("proto", r.Proto)
 	enc.AddString("method", r.Method)
 	enc.AddString("host", r.Host)