How to solve collisions between Lager and OTP's Logger in Erlang?

870 Views Asked by At

I have an Erlang project in OTP 22 which uses the built-in logger module for logging (which was added in OTP 21).

My project uses the dependency "amqp_client" (https://github.com/rabbitmq/rabbitmq-erlang-client).

This dependency uses lager library for logging, so lager is also one of the dependencies in my project.

From the moment I've added lager to my project, the logs that were done via logger are not working anymore (I guess that's because lager is overriding some logging-handler of the VM which logger also uses, or something like this?)

Anyone has an idea?

2

There are 2 best solutions below

1
On

EDIT: I misread the question, it is about Erlang, not Elixir.


See the troubleshooting section of amqp:

Lager conflicts with Elixir logger

Lager is used by rabbit_common and it is not Elixir's best friend yet. You need a workaround.

In mix.exs, you have to load :lager before :logger.

extra_applications: [:lager, :logger, :amqp]

Here is a sample configuration to silent rabbit_common logging.

config :lager,
 error_logger_redirect: false,
 handlers: [level: :critical]

Check out Lager and RabbitMQ documentation for more information.

0
On

(This should be a comment to the answer provided by evnu, but I don't have the rep required for comment yet)

In lager you can see how it removes the default logger handler. You have 2 options if you leave error_logger_redirect on:

  1. Rely on lager for everything (configure lager for the whole project)
  2. Add your own logger handler (you can do it in the sys.config, this is what I do for my projects)

Untested sys.config example for 2:

[
    {kernel, [
        {logger, [
            {handler, my_default, logger_std_h, #{
                level => debug,
                config => #{
                    sync_mode_qlen => 200,
                    overload_kill_enable => true
                },
                formatter => {logger_formatter, #{template => [time," ",pid," ",level,": ",msg,"\n"]}}
            }}
        ]}
    ]}
].