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.
22 lines
644 B
Elixir
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
|