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.
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
--