ExUnit v1.7.3 ExUnit.CaptureLog View Source
Functionality to capture logs for testing.
defmodule AssertionTest do use ExUnit.Case import ExUnit.CaptureLog require Logger test "example" do assert capture_log(fn -> Logger.error "log msg" end) =~ "log msg" end test "check multiple captures concurrently" do fun = fn -> for msg <- ["hello", "hi"] do assert capture_log(fn -> Logger.error msg end) =~ msg end Logger.debug "testing" end assert capture_log(fun) =~ "hello" assert capture_log(fun) =~ "testing" end end
Link to this section Summary
Link to this section Functions
Captures Logger messages generated when evaluating
Returns the binary which is the captured output.
This function mutes the
:console backend and captures any log
messages sent to Logger from the calling processes. It is possible
to ensure explicit log messages from other processes are captured
by waiting for their exit or monitor signal.
capture_log does not guarantee to capture log messages
originated from processes spawned using a low level
Kernel.spawn/1) and such processes exit with an
exception or a throw. Therefore, prefer using a
Task, or other OTP
process, will send explicit logs before its exit or monitor signals
and will not cause VM generated log messages.
Note that when the
async is set to
true, the messages from another
test might be captured. This is OK as long you consider such cases in
It is possible to configure the level to capture with
which will set the capturing level for the duration of the
capture, for instance, if the log level is set to :error
any message with the lower level will be ignored.
The default level is
nil, which will capture all messages.
The behaviour is undetermined if async tests change Logger level.
The format, metadata and colors can be configured with
:colors respectively. These three options
defaults to the
:console backend configuration parameters.