Unit-testing – Should log statements be tested

loggingtestingunit testing

When writing tests I usually ignored logging statements, but now I wonder if it was right.

The logs are often important tools when diagnosing production issues, moreover there can even be requirements for logs, like "log every interaction with external system, including request and response on highest detail level" or "never ever log unmasked sensitive data".

From my experience such requirements were tested by the functional testers sifting through pile of logs with some scripts. But maybe it would be better to ensure the logging is correct closer to code – with unit tests.

Now the question is should the logs be tested with unit tests?

I'm asking because I feel that often unit testing logging would require considerable effort because

  • Often involves parsing stream of strings
  • Capturing the logs in test case in may be not straightforward
  • Capturing logs in a way that that ensures they ar not interlaced with logs from other sources may be even less straightforward

On the other hand I believe quality of logging in application is important and unit testing may be a way to ensure that quality, but maybe other ways are more cost effective.

Best Answer

Yes you should absolutely test your logging. I said logging and not logs because they don't matter much, as logs are a detail of implementation.

What I mean is you should test that the action of logging was done. Either by using a mock, a fake or anything you'd like. You'll find it a lot easier to test that behavior, and your code will also be cleaner as it'll depend on the concept of logging, not the log files concretely. That means you'll be able to change your strategy of logging as a whole depending on anything you'd like (hint: testing environment, cough, fake logging in memory, cough, easy assertions).

In case you come to think your logging is not tested "for real" (and you will), understand that only the implementing class(es) of that logging concept should be tested "for real". Not classes which use the logger.

So now classes using the fake logger prove they're using it the way they should, and implementations of the logger prove they work.