Can a return value be specified from capture_logs function

I have the following at the top of my test suit which currently throws an error

use Raxx.Verify.RequestCase

  import ExUnit.CaptureLog, only: [capture_log: 1]

  setup do
    raxx_app = {Raxx.Verify.Forwarder, %{target: self()}}
    capture_log fn() ->
      {:ok, endpoint} = Ace.HTTP.start_link(raxx_app, port: 0)
      {:ok, port} = Ace.HTTP.port(endpoint)
    end
    {:ok, %{port: port}} # I want to return from my setup the value of the port.
  end

Is there a simple way to get the value of that port out of the function.
In my case starting up a server is quite noisy and I do not want the startup messages in my test logs.

Maybe easier to quieten the log? I don’t know the log level that Ace logs at but something like this in your config/test.exs:

config :logger
  compile_time_purge_level :info. # or :warn, or :error

Which may allow you to simply:

  import ExUnit.CaptureLog, only: [capture_log: 1]

  setup do
    raxx_app = {Raxx.Verify.Forwarder, %{target: self()}>
    {:ok, endpoint} = Ace.HTTP.start_link(raxx_app, port: 0)
    {:ok, port} = Ace.HTTP.port(endpoint)
    {:ok, %{port: port}} # I want to return from my setup the value of the port.
  end

Could also do this without changing the config file:

  import ExUnit.CaptureLog, only: [capture_log: 1]

  setup do
    Logger.configure(level: :error)
    raxx_app = {Raxx.Verify.Forwarder, %{target: self()}>
    {:ok, endpoint} = Ace.HTTP.start_link(raxx_app, port: 0)
    {:ok, port} = Ace.HTTP.port(endpoint)
    {:ok, %{port: port}} # I want to return from my setup the value of the port.
  end

Thanks for that,

I don’t want to quieten the logs globally as I see stdout output as part of the interface an I have other tests to check that the correct content is written in some cases.

The second option looks good cheers.
At the moment I have a solution where I send the config I want to self() and pick it up from the mailbox but that might not be the best solution

I prefer to use:

ExUnit.start(capture_log: true)

Which captures all logging and outputs on a failure.

I only use the capture_log/2 function only when I want to assert or refute output.

3 Likes