Logs are an important part of a developer’s toolbox, we rely on them to understand how an application is behaving or misbehaving. Error logs are particularly interesting, as they indicate that something wrong happened, possibly requiring the developers to react, investigate and fix some issue. In practice, error logs are one of the first pieces of information that an on-call engineer would look for when they receive a page. Being so important for understanding anomalous situations, how can we make sure that errors are actually logged?
Testing error logs is the practice I recommend as an answer to this question. Have an assertion in your test specifically verifying that your logging library was used for logging an error. As a developer who first writes the tests, it will force me to log errors while implementing, and at the same time, it will avoid the log to be accidentally removed in the future.
Some logging libraries will make it easier for you to create assertions over logs, but others can make it a troublesome task. In the latter case, you’ll need to decide how much time are you willing to invest to find a way to make it work, be it a workaround or a contribution to the library itself. I recently found myself attempting to test logs with logback
, a logging library for Java/Kotlin applications, using JUnit
as testing framework. In this post I share with you how I managed to make it work for that particular tech stack.
Check out this article in Martin Fowler’s blog if you want to read more about testing observability and see a step-by-step code example.