ExLoggerMock v1.1.1 ExLoggerMock.Backend View Source

A logging backend for unit testing.

Configure in your config/test.exs to replace the default :console backend:

config :logger,
  backends: [{ExLoggerMock.Backend, :ex_logger_mock}]

This will suppress all log output on the console during mix test.

It's simple to assert a certain log message happened in your unit test assertions:

assert_receive {:ex_logger_mock, {:info, "test log message", _timestamp, _metadata}}

Link to this section Summary

Functions

ignore any 'call' notification, including :configure

Handle the log 'event'.

intialise from configuration, but we have no config

Link to this section Functions

Link to this function

handle_call(request, state)

View Source
handle_call(any(), any()) :: {:ok, :ok, any()}

ignore any 'call' notification, including :configure

Link to this function

handle_event(arg1, state)

View Source
handle_event({atom(), pid(), {atom(), binary(), tuple(), list()}}, map()) ::
  {:ok, map()}

Handle the log 'event'.

Send a message to the original process that created the log event. This is particularly useful in testing scenarios when testing code like:

Logger.warn("test warning message")

You can verify that call happened with test code like:

assert_receive {:ex_logger_mock, {:warn, "test warning message", _timestamp, _metadata}}

NOTE: use assert_receive, not assert_received because logging happens asynchronously across processes; need to give the test response message a short time to arrive.

The third element in the tuple (timestamp) is probably useless in a testing scenario.

The fourth element (metadata) is pulled, untouched, from the incoming log data.

Do nothing if this process is not the 'group leader'.

Do nothing if the event is not a log event.

Link to this function

init(arg)

View Source
init({ExLoggerMock.Backend, binary()}) :: {:ok, %{name: binary()}}

intialise from configuration, but we have no config