[ExUnit.CaptureLog] Support for custom formatters?

14 views
Skip to first unread message

Austin Ziegler

unread,
Nov 13, 2025, 8:25:52 PMNov 13
to elixir-l...@googlegroups.com
I recently did a deep dive into ExUnit.CaptureLog and found that it (mostly) reverted to the default formatter with no way to specify an alternative formatter such as LoggerJSON to be able to test certain advanced features like the LoggerJSON.Redactor protocol to ensure that fields logged are properly redacted.

The (mostly) is because at work we have a formatter that's a variation of the default formatter and we (mostly) get the output that we expect (the default formatter drops any metadata that isn't a scalar; our custom formatter does best-effort serialization), but we didn't find a way to provide an entirely alternative formatter like LoggerJSON.

So I copied the relevant files from the Elixir source code and created CaptureLogger. https://hexdocs.pm/capture_logger/CaptureLogger.html

The core change is in the call handler for `:log_capture_on`:

```elixir
    {formatter, opts} = Keyword.pop(opts, :formatter, Application.get_env(:capture_logger, :formatter))

    {formatter_mod, formatter_config} =
      case formatter do
        {_, _} -> formatter
        nil -> Logger.default_formatter(opts)
        module when is_atom(module) -> module.new(opts)
      end
```

I'd be happy to contribute a similar change for future versions of Elixir, but CaptureLogger will work nicely for earlier versions until this gets released.

WDYT?

-a
--

José Valim

unread,
Nov 14, 2025, 2:25:04 AMNov 14
to elixir-l...@googlegroups.com
We would gladly allow the formatter as an option in `capture_log`!


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/elixir-lang-core/CAJ4ekQu33Bk9955ZCvmsyLChq3OKm2hH0pwcTHDwA14ckd0umg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages