defmodule ToweropsWeb.Plugs.RemoteIpLogger do @moduledoc """ Plug that extracts the remote IP address and adds it to Logger metadata. When behind a proxy/load balancer (like Traefik in Kubernetes), the real client IP is in the X-Forwarded-For or X-Real-IP headers. This plug extracts the IP and adds it to Logger metadata so it appears in all logs for the request. """ alias ToweropsWeb.RemoteIp require Logger def init(opts), do: opts def call(conn, _opts) do remote_ip = RemoteIp.from_conn(conn) # Add to Logger metadata so it appears in all logs for this request Logger.metadata(remote_ip: remote_ip) conn end end