PJUllrich

PJUllrich

Author of Building Table Views with Phoenix LiveView

I’ve been trying to add a custom logger backend to my application using the now-recommended :logger.add_handler/3 function as suggested to me by @LostKobrakai (on Twitter). I’ve ran into an issue with the config of the handler, which changes between adding the handler and logging the first event. This is my logger:

defmodule Demo.FileLogger do
  def adding_handler(config) do
    IO.inspect(config, label: "Initial")
    {:ok, config}
  end

  def log(event, config) do
    IO.inspect(event, label: "Event")
    IO.inspect(config, label: "Config")

    :ok
  end
end

This is how I add the handler to my application:

# lib/demo/application.ex
  @impl true
  def start(_type, _args) do
    :ok = :logger.add_handler(:file_log, Demo.FileLogger, %{})
    # ...
end

Now, the handler logs the following config in the adding_handler/1 callback:

Initial: %{
  id: :file_log,
  module: Demo.FileLogger,
  level: :all,
  filters: [],
  filter_default: :log,
  formatter: {:logger_formatter, %{}}
}

but once the log/2 callback is executed for the first time, config only contains the following values:

Config: %{
  id: :file_log,
  module: Demo.FileLogger,
  formatter: {:logger_formatter, %{}}
}

Note the missing level, filters, and filter_default fields. This is surprising to me, because in the “old” :gen_event behaviour for logger backends, you’d set a config during the init/1 callback and this config would be passed to every callback thereafter, just like in a GenServer. Why is this different in the :logger backend and what’s the suggested solution to pass on a config map between all callbacks?

I also implemented the changing_config/2 callback to see whether the config is changed somehow, but it never executes between the adding_handler/1 and the log/2 callback.

Showing Posts 1 to 5

cmo

cmo

Doesn’t the config go under a config key?

%{
  config: %{ level: :error, ...}
}
PJUllrich

PJUllrich OP

Author of Building Table Views with Phoenix LiveView

Yes, I think you’re right. At least that’s what worked for me now. It says so in the docs, too:

Handler specific configuration data is inserted by the handler callback itself, in a sub structure associated with the field named config.

From: logger_handler — OTP 29.0.2 (kernel 11.0.2)

However it’s still confusing because e.g. the spec for the config-type contains all these fields and is references both in the adding_handler/1 and log/2 callbacks, but it’s not the same map in these cases. But anyways. Handler-specific configurations should go under the config.config (also a great naming :D) key I presume.

cmo

cmo

I found it pretty confusing when I moved to the new erlang logger setup. I think the docs need some love in this area.

PJUllrich

PJUllrich OP

Author of Building Table Views with Phoenix LiveView

Yeah, I’m currently battling with converting the old Logger.Formatter.format/5 to :logger_formatter.format/2. Especially converting the old format strings like "$date $time [$level] $message to the new form [:date, " ", :time, " [", :level, "] ", :msg, "\n"] is non-trivial. I appreciate the effort of consolidating the two logging interfaces, but when it comes to extensibility, I’m afraid this is a step backwards.

PJUllrich

PJUllrich OP

Author of Building Table Views with Phoenix LiveView

I managed to write a little helper function that’d convert the old format string to the new template-option format: run-elixir/assets/archived_code/file_logger.ex at 85dd2599d9185c259c03f1226912b48e62e73e8c · PJUllrich/run-elixir · GitHub

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews