MarcN

MarcN

Slow Logger backend slowing down other backends even in async mode?

Recently we have been running into an issue where one slow Logger backend is slowing down another Logger backend. This happens even when in async mode when there are only a few messages to log, so it seems unrelated to sync_threshold which was my first thought.

I made a small Mix project showing the behaviour (I tried this in the current elixir:latest image, which has Erlang/OTP 26 and Elixir 1.15.7). The project defines two simplistic Logger backends: MyQuickBackend and MySlowBackend. Both just log to the console using IO.puts but the slow backend then sleeps for 2 seconds after handling a message (to simulate a slow logger, for example logging to a slow remote server). The application itself simply logs 5 warnings. Here are the files:

File tree:

+ config
|--config.exs
+ lib
|--my_quick_backend.ex
|--my_slow_backend.ex 
|--project.ex
mix.exs
mix.lock

In mix.exs:

defmodule Project.MixProject do
  use Mix.Project

  def project do
    [
      app: :project,
      version: "0.1.0",
      elixir: "~> 1.15",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  def application, do: [mod: {Project, []}, extra_applications: [:logger]]
  defp deps, do: [{:logger_backends, "~> 1.0.0"}]
end

In mix.lock:

%{
  "logger_backends": {:hex, :logger_backends, "1.0.0", "09c4fad6202e08cb0fbd37f328282f16539aca380f512523ce9472b28edc6bdf", [:mix], [], "hexpm", "1faceb3e7ec3ef66a8f5746c5afd020e63996df6fd4eb8cdb789e5665ae6c9ce"},
}

In config/config.exs:

import Config

config :logger, backends: [:console, MySlowBackend, MyQuickBackend]

In lib/my_slow_backend.ex:

defmodule MySlowBackend do
  def init(_) do
    {:ok, %{}}
  end

  def handle_event({_level, _gl, {Logger, msg, _ts, _meta}}, state) do
    IO.puts("MySlowBackend: #{msg} - now sleeping")
    Process.sleep(2000)

    {:ok, state}
  end

  def handle_event(:flush, state) do
    {:ok, state}
  end
end

In lib/my_quick_backend.ex:

defmodule MyQuickBackend do
  def init(_) do
    {:ok, %{}}
  end

  def handle_event({_level, _gl, {Logger, msg, _ts, _meta}}, state) do
    IO.puts("MyQuickBackend: #{msg}")

    {:ok, state}
  end

  def handle_event(:flush, state) do
    {:ok, state}
  end
end

In lib/project.ex:

defmodule Project do
  require Logger
  use Application

  def start(_start_type, _start_args) do
    1..5 |> Enum.each(fn n -> Logger.warning("Warning #{n}") end)

    Supervisor.start_link([], strategy: :one_for_one)
  end
end

Run with mix do deps.get, run.

What I see happening is to following: the :console backend logs these 5 warnings instantly as expected, and the MySlowBackend backend logs the warnings at two-second intervals as expected. However, the MyQuickBackend backend is also handling the warnings are two-second intervals, which I did not expect at all. Note that the 5 warnings is far below the default sync_threshold, and in any case, increasing this threshold does not make this problem go away.

Why does this happen? Is this expected behaviour? Am I doing something wrong?

Most Liked

ausimian

ausimian

I’m guessing (and also typing this on my phone) but is it possible that :console is an erlang :logger handler in contrast to your backend handlers which are Elixir Logger handlers? They go through different paths I think, but prolly better if someone who actually knows chimes in.

josevalim

josevalim

Creator of Elixir

@ausimian is 100% correct in both replies.

ausimian

ausimian

I think Elixir’s backends run as :gen_event handlers in a single process. Each handler’s callback is processed serially within the Logger’s event manager. This is also why, given your test of 5 successive messages, the order doesn’t matter.

I suspect if you rewrote the slow handler to forward messages to another process, the fast handler would no longer be affected in the same way.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
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
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement