# ExLoggerMock

A mock logging backend for Elixir unit tests. A clean way to get rid of log output in the test run.

## Installation

The package can be installed by adding `ex_logger_mock` to the list of dependencies in `mix.exs`:

def deps do
    {:ex_logger_mock, "~> 1.0", only: :test, runtime: false}

Documentation can be found at

## Configuration

In the `test` environment configuration, replace the default `:console` logging backend with this
package in your `config/test.exs` file:

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

Some packages, e.g. Rollbax, don't like receiving unexpected messages. You can limit the applications
that will be sent a message with extra configuration:

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

config :ex_logger_mock, application_filter: [:my_app, :my_app_web]

## Use

The log calls themselves do not change. Without the `:console` backend, you won't see log output
during the `mix test` run.

In unit tests, you can now assert for specific messages being logged:

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

NOTE: `assert_receive` here, not `assert_received`. Logging happens asynchronously across processes,
so we need to give the message a little time to make it's way back to the test process.

## Why

I don't like seeing log output during the `mix test` run. Traditionally, we solved that problem by
setting the log level in the test configuration to `:warn`. But that solution makes testing "normal"
log output with `log_capture` fail. Logs at `:info` level won't ever be produced, so cannot be

Inspired by [José's Mocks and explicit
contracts]( post, and with
many thanks to Brandon Richey for his [The Ultimate Guide To Logging In
Elixir]( post, this package solves
both those problems.

## Contributing

The typical fork, branch and PR dance.

To check all is well:

mix compile --force --warnings-as-errors && \
mix coveralls && \
mix credo --strict

## License

MIT - do what you want with it.