Can you set different log levels for different routes in Phoenix?
In more recent versions of Phoenix it looks like that specific log is now produced by Phoenix.Logger
in response to telemetry events emitted by Plug.Telemetry
. Suppressing specific routes can be done in very much the same fashion as with Plug.Logger
. Either create a separate endpoint / pipeline that does not include telemetry at all, or provide a custom implementation of the plug that alters the log level for certain paths:
defmodule MyWeb.Plugs.Telemetry do
@behaviour Plug
@impl true
def init(opts), do: Plug.Telemetry.init(opts)
@impl true
def call(%{path_info: ["ping"]} = conn, {start_event, stop_event, opts}) do
Plug.Telemetry.call(conn, {start_event, stop_event, Keyword.put(opts, :log, :debug)})
end
def call(conn, args), do: Plug.Telemetry.call(conn, args)
end
and then replace
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
with
plug MyWeb.Plugs.Telemetry, event_prefix: [:phoenix, :endpoint]
in your endpoint.ex
.
You can define a plug that calls Plug.Logger
with a different level for different paths.
defmodule MyApp.Logger do
def init(_opts), do: {}
def call(%{path_info: ["ping"]} = conn, _opts) do
Plug.Logger.call(conn, :error)
end
def call(conn, _opts) do
Plug.Logger.call(conn, :info)
end
end
Now replace plug Plug.Logger
with plug MyApp.Logger
in your Endpoint
module. All requests to /ping
will now log at :error
level while everything else will log at info
level.