towerops/lib/towerops_web/plugs/remote_ip_logger.ex
Graham McIntire 4421727e61 fix: resolve all Elixir 1.20 type system warnings across project
Removes unreachable catch-all clauses, drops unused `require Logger`
lines, adds pin operators to bitstring size patterns, reorders function
heads for correct dispatch, and replaces deprecated LoggerBackends calls.
2026-05-28 14:52:30 -05:00

22 lines
644 B
Elixir

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
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