I have an endpoint that’s used to check to see if an instance is up and available (for use with a node balancer). The node balancer will hit this endpoint once a second or so. Obviously that produces a lot of unnecessary log chatter, so I’m trying to figure out how to disable the request logging for this endpoint only.
I noticed that in Phoenix.Controller there’s an option under Plug Pipeline called “log” - but I’m not sure if it’s relevant and how to use it if it is.
I know this post has been a while but I have the same situation like this now.
I have a health_check controller which receives ping every xx secs, and would be ideal the logger to be turned off for it.
But the current phoenix version does not have Plug.Logger in endpoint.ex any more so I don’t where to disable it and then apply it in router.ex via different pipeline.
defmodule HelloWeb.Plug.HealthCheck do
import Plug.Conn
def init(opts), do: opts
def call(%Plug.Conn{request_path: "/healthy"} = conn, _opts) do
conn
|> send_resp(200, "")
|> halt()
end
def call(conn, _opts), do: conn
end
And then endpoint.ex uses it like this plug(HelloWeb.Plug.HealthCheck) as the first thing that gets defined. This way it does the least amount of work possible. No controller or route needed and your normal pipeline gets ignored.
Pretty sure I grabbed that snippet and slightly modified it from one of @jola’s blog posts.
Define a custom plug to replace Plug.Logger in endpoint.ex:
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
I just ran into this. I believe the scope option is not about those GET / and Sent 200 in 286µs logs, it’s about these debug logs you see:
[debug] Processing with PhxDisableLogWeb.PageController.index/2
Parameters: %{}
Pipelines: [:browser]
You will notice that the log: false option on scope does disable these log entries.
The other request logs are disabled by adding log: false to the Plug.Telemetry plug (in endpoint.ex), but this then applies to all routes. I am not aware of an approach to disable these on a per request basis (without resorting to your own logging).
Using Unplug, you can provide a predicate module that gets evaluated under the context of the current plug, and you can specify whether you want the attached plug to be executed or not.
The Telemetry plug now accepts a callback that makes this very easy. There is a full example in the Phoenix docs (dynamic log level): Phoenix.Logger — Phoenix v1.7.12