Custom Formatters
Explore creating custom logger formatters in Elixir to output JSON logs with metadata, handling edge cases to avoid crashes. Understand how to build custom backends for error tracking, crucial for monitoring production systems and ensuring reliable debugging.
We'll cover the following...
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:
Create a new IEx session with the following:
Then, log any message, like this:
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 ...