In this post I want to show how easy it is to extend the functionality towards logging based on a verbosity level for the application.
The verbosity level is normally given via the command line. Writing a CLI is not part of this post, so to make it simple the application will take just one argument: the verbosity level given as a number.
Our idea is very simple: Instead of just providing a list of strings we now provide a list of tuples, each tuple consisting of an importance level and the actual string. The lower the importance level, the higher the importance, i.e. it is inverse propertional to the verbosity level.
The main code can remain almost unchanged. All we have to do is read the verbosity level from our command line and filter the log based on the log level:
Here is the full code for the example.
Nothing that couldn’t be solved, but this is not the purpose here. I want to stress that the actual changes necessary to achieve the new functionality required less than 10 lines of code!