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?
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project.
My initial shotgu...
New
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
Other Trending Topics
As part of building a service for automatically publishing blog posts from RSS feeds into atproto’s standard.site lexicon, I implemented ...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Chat & Discussions>Discussions
Latest on Elixir Forum
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
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










First Post!
axelson
Hi, while I’m not on the Elixir Core Team I wanted to chime in here to say that I think this is a good idea and it could simplify a common case of wanting to assert on the result of a statement as well as any logs that it generates. In particular I like the fact that it would reduce the visual code changes that are introduced when you want to assert on the log result. Currently you need to move your assert on the result into the
capture_loganonymous function which makes it a little less clear (generally I prefer to keep all the asserts at the top-level).In terms of the API I think that passing
with_result: truewould be a little more discoverable than a totally new function (but the core team may have different opinions of course).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_resultor something more concise (could we replacecaptureby 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!
Last Post!
hauleth
It is what I often done. Sometimes I also have sent message to
self()with result to access it outside the closure.