marcandre

marcandre

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?

Showing Posts 1 to 10

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

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

with_logs

dimitarvp

dimitarvp

  • bag
  • grab
  • seize
  • ensnare
  • entomb
  • nab

But yep, human languages, especially English, are imperfect.

hauleth

hauleth

Seize means of logging for the debugging class!

dimitarvp

dimitarvp

And entomb the enemies of observability!

josevalim

josevalim

Creator of Elixir

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

marcandre

marcandre OP

Awesome! Sounds really easy, I’ll do it :+1:

astery

astery

Thank you, @marcandre, it’s already merged to master.

ityonemo

ityonemo

why not assert inside the nested fun?

    assert ExUnit.CaptureLog.capture_log(fn ->
      result = fun.()
      assert result == "foo"
    end) =~ match

Where Next? Top

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...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews