I’m trying to use flatlog, in a mix project.
https://github.com/ferd/flatlog#usage
I have found that I need to add the following to config.exs
config :logger, handle_otp_reports: false
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”}…
But I can’t work out how to modify usage instructions on flat log so they work in config.exs.
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”}…
You need to disable Elixir’s logger with the instruction given in the other post and then add your handler to Erlang logger, either directly via :logger.add_handler/3 or via :logger.add_handlers/1 and application configuration. So in the end your config will look like:
config :logger, handle_otp_reports: false
config :my_app, :logger, [
{:handler, :default, :logger_std_h,
%{formatter: {:flatlog, %{
map_depth: 3,
term_depth: 50
}}}
}
]
And in your Application.start/2 callback you need to do:
:logger.add_handlers(:my_app)
Is there not a way to generate the appropriate sys.config so I don’t need to make the call in application.start?
Not in development. In production you can use:
config :kernel, :logger, [
{:handler, :default, :logger_std_h,
%{formatter: {:flatlog, %{
map_depth: 3,
term_depth: 50
}}}
}
]
And it should work as expected. Unfortunately Elixir do not reload kernel configuration after it starts, so in development it is not possible to change configuration options for it.