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.

First Post!

cmo

cmo

Doesn’t the config go under a config key?

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

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

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement