marcandre
Feature request: Could `capture_log/2` also return the result of the function?
Currently, ExUnit/CaptureLog.capture_log/2 returns the logs content captured but offers no possibility of returning any other result.
This makes it difficult to execute some_function we know should emit a log and both assert the log content and use the result of some_function.
One alternative is using the result within the function passed to capture_log, but that doesn’t compose well, or nest, and is problematic as the rest of the code could generate warnings, etc.
Another way is uses the Process dictionary, but that’s pretty ugly:
def with_log_matching(match, fun) do
assert ExUnit.CaptureLog.capture_log(fn ->
result = fun.()
Process.put(:log_capture_function_result, result)
end) =~ match
Process.delete(:log_capture_function_result)
end
test "something" do
result = with_log_matching("Hey, something strange is going on", fn ->
do_something_and_return(...)
end
assert do_something_else(result) == :ok
end
Could we have either a new function capture_log_and_result, or else a new option for capture_log like with_result: true or similar that would return {result, "captured log..."} instead of just the captured log?
Most Liked
josevalim
+1 that this feature would be desired. I don’t like though using an option to change the return type. My suggestion would be to introduce a new function, as the mentioned capture_log_and_result or something more concise (could we replace capture by a word that would have such meaning?).
hauleth
Seize means of logging for the debugging class!
josevalim
I like with_log and with_io (from the equivalent capture_io). A PR is welcome!
Popular in Discussions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









