Custom Formatters

Learn and practice the custom formatters and error handling using custom backends.

Using the custom formatters

So far, we’ve learned how to configure our logger level to :warn and guarantee the :debug and :info messages are as cheap as possible. Logger is ready to support our custom needs. The external system expects all log messages to be in a specific format—one that might not match our application. Luckily, Logger supports custom formatters. To see one in action, let’s create a new Elixir application. We have created one for you below:

Press + to interact
mix new formatter

Create a new IEx session with the following:

Press + to interact
iex -S mix

Then, log any message, like this:

Press + to interact
Logger.error("hello")

Next, we want to log messages as JSON and include as much metadata as possible. To do so, we need to write our own format. We need to bring some dependencies into the project so we open up mix.exs and add ...