如何解决Erlang中Lager和OTP的Logger之间的冲突?

yruzcnhs  于 2022-12-08  发布在  Erlang
关注(0)|答案(2)|浏览(267)

我在OTP 22中有一个Erlang项目,它使用内置的logger模块进行日志记录(在OTP 21中添加)。
我的项目使用依赖项“amqp_client”(https://github.com/rabbitmq/rabbitmq-erlang-client)。
这个依赖项使用lager库进行日志记录,所以lager也是我项目中的依赖项之一。
从我将lager添加到我的项目的那一刻起,通过logger完成的日志就不再工作了(我猜这是因为lager覆盖了logger也使用的VM的一些日志处理程序,或者类似的事情?)
有人有主意吗?

1qczuiv0

1qczuiv01#

(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"]}}
            }}
        ]}
    ]}
].
pkwftd7m

pkwftd7m2#

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.

相关问题