Skip to content

Conversation

@halostatue
Copy link
Contributor

Mostly modified :log_capture_on handler for ExUnit.CaptureServer to respect a :formatter option instead Logger.default_formatter/1.

Updates were made to documentation and tests to reflect this change.

@halostatue
Copy link
Contributor Author

This is a minimal adaptation of the changes I made when publishing the proof-of-concept / backport package that I created for this, CaptureLogger. In CaptureLogger I'm testing with LoggerJSON, but I've created a much more simplistic "ReverseLogger" that stringifies and reverses the message being logged.

In CaptureLogger, I am using a default formatter possibly pulled from Application.get_env/2. It feels like we could pull from a possibly configured Application.get_env(:logger, :default_handler, %{})[:formatter] value here but that would make this a breaking change for anyone using ExUnit.CaptureLog with a configured formatter in the default handler (e.g., they're using LoggerJSON). Alternately, we could have a new config (Application.get_env(:ex_unit, :capture_log_default_formatter)). Not sure but open to suggestions.

I also think that I could duplicate one of the tests so that it shows the use of leftover options, as I'm showing two variations of {module, opts} (one of them with module.new/1). Let me know if you want that.

@halostatue halostatue force-pushed the ex_unit_capture_log_formatter branch from 4544fc3 to bce6200 Compare November 16, 2025 01:50
Mostly modified `:log_capture_on` handler for ExUnit.CaptureServer to
respect a `:formatter` option instead `Logger.default_formatter/1`.

Updates were made to documentation and tests to reflect this change.
@halostatue halostatue force-pushed the ex_unit_capture_log_formatter branch from bce6200 to 9ba53f8 Compare November 16, 2025 01:51
Copy link
Member

@whatyouhide whatyouhide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely

@josevalim josevalim merged commit 5732283 into elixir-lang:main Nov 17, 2025
12 of 13 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

{formatter_mod, formatter_config} = Logger.default_formatter(opts)

{formatter_mod, formatter_config} =
Keyword.get_lazy(opts, :formatter, fn -> Logger.default_formatter(opts) end)
Copy link
Contributor

@sabiwara sabiwara Nov 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the spec, formatter can be {module(), term()} | nil, but wouldn't this fail in the nil case?

Should it be Keyword.get(opts, :formatter) || Logger.default_formatter(opts), or should we fix the spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I can make either change.

@halostatue halostatue deleted the ex_unit_capture_log_formatter branch November 17, 2025 23:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants