PJUllrich

PJUllrich

Author of Building Table Views with Phoenix LiveView

:logger.add_handler/3 sets an unexpected config map

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.

Most Liked

PJUllrich

PJUllrich

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

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

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.

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
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
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
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
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
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement