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?

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!

Where Next?

Popular in Discussions Top

blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
WildYorkies
It seems that the more I read, the more I find Elixir users speaking about all the ways that Elixir is not good for x, y, and z use cases...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19568 166
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement