Add user-agent to request logging metadata

This commit is contained in:
Graham McIntire 2026-06-04 16:19:47 -05:00
parent 6d190bc98e
commit 5569d8e2a0
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59
3 changed files with 13 additions and 3 deletions

View file

@ -131,7 +131,7 @@ config :esbuild,
# Configures Elixir's Logger # Configures Elixir's Logger
config :logger, :console, config :logger, :console,
format: "$time $metadata[$level] $message\n", format: "$time $metadata[$level] $message\n",
metadata: [:request_id, :remote_ip] metadata: [:request_id, :remote_ip, :user_agent]
# Use Jason for JSON parsing in Phoenix # Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason config :phoenix, :json_library, Jason

View file

@ -85,7 +85,7 @@ config :hammer,
# The `http:` config above can be replaced with: # The `http:` config above can be replaced with:
# #
# https: [ # https: [
config :logger, :console, format: "[$level] $metadata$message\n", metadata: [:remote_ip] config :logger, :console, format: "[$level] $metadata$message\n", metadata: [:remote_ip, :user_agent]
# Initialize plugs at runtime for faster development compilation # Initialize plugs at runtime for faster development compilation
# port: 4001, # port: 4001,

View file

@ -22,7 +22,17 @@ defmodule AprsmeWeb.Plugs.RemoteIp do
:error -> conn :error -> conn
end end
Logger.metadata(remote_ip: conn.remote_ip |> :inet.ntoa() |> to_string()) user_agent =
case get_req_header(conn, "user-agent") do
[ua | _] -> ua
[] -> "-"
end
Logger.metadata(
remote_ip: conn.remote_ip |> :inet.ntoa() |> to_string(),
user_agent: user_agent
)
conn conn
end end