johnhamelink

johnhamelink

Disable logging on specific route?

Hey there,

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.

Anyone else figured this out before?

Marked As Solved

johnhamelink

johnhamelink

I figured it out :slight_smile: thanks to @benwilson512 and others!

The answer is to use pipelines:

  • Remove the Plug.Logger line from the endpoint.ex file.
  • Add a pipeline which doesn’t include the Logger
  • Add plug Plug.Logger to all other pipelines
  • Add pipe_through <pipeline which doesn't include logger> to the scope you don’t want to log.
17
Post #2

Also Liked

jola

jola

scope log: false do
  get "/pages/:id", PageController, :show
end

or

get "/pages/:id", PageController, :show, log: false

https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/2
https://hexdocs.pm/phoenix/Phoenix.Router.html#get/4

17
Post #4
cnck1387

cnck1387

I use this plug:

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.

10
Post #7
akoutmos

akoutmos

Author of Build a Weather Station with Elixir and Nerves

For anyone that stumbles across this question in the future, I wrote a library to do this exact thing but with any plug GitHub - akoutmos/unplug: Unplug allows you to conditionally execute your Elixir plugs at run-time

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.

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42842 311
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43591 214
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35953 110
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement