1 min read

Disabling Phoenix.Logger for specific endpoints

We use pretty much the default Phoenix.Logger setup for the most part, with a custom JSON formatter. Recently while investigating some prod issues I realised that my default first action when opening the logs in grafana is to filter out the healthcheck endpoint logs. Each one gets a request and response entry which make it really tricky to see what's actually going on.

It turns out, disabling this is really very easy.

In your endpoint.ex module, you can use dynamic log levels with Plug.Telemtry and then force the log level to false for your healthcheck endpoint. Here's ours which removes the entries for our GET /health endpoint.

plug Plug.Telemetry,
    event_prefix: [:phoenix, :endpoint],
    log: {__MODULE__, :log_level, []} # <-- call a local function for dynamic log_level assignments

def log_level(%{path_info: ["health" | _]}), do: false # Return `false` for the health endpoint
def log_level(_), do: :info # Default to `info` otherwise

As commented in the code, this uses a local function for dynamic level assignments. It then returns a false log level for the health endpoint (which disables those log entries) and otherwise defaults to info.