How to use erlang's logger library instead of elixir default one?

How can I use erlang’s logger library instead of the elixir default one? I wanted to try it since it has structure logging which is kinda nice.

I tried simply using :logger directly, this does work, but the output is different from when I run the same command in erlang directly:
elixir:

iex(1) > :logger.error %{id: 123, from: :module, reason: “reason”}
22:12:04.078 [error] [from: :module, id: 123, reason: “reason”]

erlang:

2> logger:error(#{id => 123, from => module, reason => “reason”}).
=ERROR REPORT==== 24-Sep-2019::22:17:19.836413 ===
from: module
id: 123
reason: reason

This is because Elixir Logger by default will replace default handler with it’s own to pass all log informations via their backends. If you want to disable that behaviour then you need to set:

config :logger, handle_otp_reports: false

It is also worth mentioning that in future there is possibility that the behaviour will slightly change as PR #9333 will make Elixir’s Logger to be thin frontend for Erlang’s Logger.

6 Likes