How to have uncaptured logger calls generate failures in ExUnit?

I was in the same situation, that I wanted to include logging in the tests. I ended up writing a little macro that gets some meta-data from __CALLER__ and generates log-tuples

assert [{:logger, :error, {4711, %{file: _, line: _}}}] = error(4711)
assert [{:logger, :warning, {'test', %{file: _, line: _}}}] = warning('test')
assert [{:logger, :debug, {[a: 1, b: 2], %{file: _, line: _}}}] = debug([a: 1, b: 2])

The advantage over capture_log is, that you can use structured logging while capture_log can only use the console backend. Also it’s absolutely pure (if you are strict and see logging as impure) so there are no problems with async. But you have to handle the log-data the core produces somehow in the shell (and do the real logging).

2 Likes