marcandre

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?

First Post! Switch mode

axelson

axelson

Scenic Core Team

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_log anonymous 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: true would be a little more discoverable than a totally new function (but the core team may have different opinions of course).

Most Liked

josevalim

josevalim

Creator of Elixir

+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

hauleth

Seize means of logging for the debugging class!

josevalim

josevalim

Creator of Elixir

I like with_log and with_io (from the equivalent capture_io). A PR is welcome!

Last Post!

hauleth

hauleth

It is what I often done. Sometimes I also have sent message to self() with result to access it outside the closure.

Where Next?

Trending in Discussions Top

AstonJ
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...
2976 91332 914
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
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
AstonJ
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
type1fool
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
juhalehtonen
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
alexslade
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 Top

jola
As part of building a service for automatically publishing blog posts from RSS feeds into atproto’s standard.site lexicon, I implemented ...
New
JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
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
ausimian
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
akoutmos
@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

We're in Beta

About us Mission Statement