This commit is contained in:
@@ -4,6 +4,14 @@ defmodule ElixirAi.Application do
|
||||
|
||||
@impl true
|
||||
def start(_type, _args) do
|
||||
# Attach custom logger that filters health checks
|
||||
:telemetry.attach(
|
||||
"phoenix-endpoint-logger",
|
||||
[:phoenix, :endpoint, :stop],
|
||||
&__MODULE__.log_request/4,
|
||||
%{}
|
||||
)
|
||||
|
||||
children = [
|
||||
ElixirAiWeb.Telemetry,
|
||||
ElixirAi.Repo,
|
||||
@@ -39,4 +47,32 @@ defmodule ElixirAi.Application do
|
||||
ElixirAiWeb.Endpoint.config_change(changed, removed)
|
||||
:ok
|
||||
end
|
||||
|
||||
# Custom request logger that filters health check endpoint
|
||||
require Logger
|
||||
|
||||
def log_request(_event, measurements, %{conn: conn}, _config) do
|
||||
# Skip logging for health check endpoint
|
||||
if conn.request_path != "/health" do
|
||||
duration = System.convert_time_unit(measurements.duration, :native, :microsecond)
|
||||
|
||||
Logger.info(
|
||||
fn ->
|
||||
[conn.method, " ", conn.request_path]
|
||||
end,
|
||||
request_id: conn.assigns[:request_id]
|
||||
)
|
||||
|
||||
Logger.info(
|
||||
fn ->
|
||||
["Sent ", to_string(conn.status), " in ", format_duration(duration)]
|
||||
end,
|
||||
request_id: conn.assigns[:request_id]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defp format_duration(μs) when μs < 1000, do: "#{μs}µs"
|
||||
defp format_duration(μs) when μs < 1_000_000, do: "#{div(μs, 1000)}ms"
|
||||
defp format_duration(μs), do: "#{Float.round(μs / 1_000_000, 2)}s"
|
||||
end
|
||||
|
||||
@@ -38,7 +38,6 @@ defmodule ElixirAiWeb.Endpoint do
|
||||
cookie_key: "request_logger"
|
||||
|
||||
plug Plug.RequestId
|
||||
plug ElixirAiWeb.Plugs.HealthCheckLogger
|
||||
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
|
||||
|
||||
plug Plug.Parsers,
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
defmodule ElixirAiWeb.HealthCheckFilter do
|
||||
@moduledoc """
|
||||
Logger filter to suppress health check endpoint logs.
|
||||
"""
|
||||
|
||||
def filter(%{meta: meta}, _config) when is_map(meta) do
|
||||
if Map.get(meta, :health_check) == true do
|
||||
:stop
|
||||
else
|
||||
:ignore
|
||||
end
|
||||
end
|
||||
|
||||
def filter(_log_event, _config), do: :ignore
|
||||
end
|
||||
@@ -1,19 +0,0 @@
|
||||
defmodule ElixirAiWeb.Plugs.HealthCheckLogger do
|
||||
@moduledoc """
|
||||
Plug that marks health check requests for filtering.
|
||||
"""
|
||||
@behaviour Plug
|
||||
require Logger
|
||||
|
||||
@impl true
|
||||
def init(opts), do: opts
|
||||
|
||||
@impl true
|
||||
def call(%Plug.Conn{path_info: ["health"]} = conn, _opts) do
|
||||
# Mark this as a health check for logger filtering
|
||||
Logger.metadata(health_check: true)
|
||||
conn
|
||||
end
|
||||
|
||||
def call(conn, _opts), do: conn
|
||||
end
|
||||
Reference in New Issue
Block a user