Common logging Mistakes and How to Avoid Them
Programming is considered by many to be technically ugly whether you are writing the code in Java, Python, Node.js or any other language. For this reason, errors can be huge whether they are logical, syntax, runtime or latent. Unless these errors are identified and fixed using debugging tools or simply going over the code manually, the program will not work.
However, in today's real-world production environment, logging information is used to identify errors, scenarios or specific events during the lifetime of an application.
Log analysis tools and processes have evolved with advancements in technology from the time of Unix to Waterfall and now DevOps where everything is automated. Instead of deploying a dozen tools, today’s log analysis requires only one tool to meet a myriad of log analysis needs.
Despite this, some logging problems still persist, especially with the lazy or inexperienced programmers. They include the following:
Logging without context
Unless you are developing a simple application, log messages without proper context have no purpose and consume space for nothing. What is the need of receiving a ‘transaction failed' message when you cannot identify where and why it happened?
Logging with context helps to ease troubleshooting. With today's increased use of log aggregators with powerful log analysis capabilities, logging with context will make a distinction between one line of code and the other.
It is good that your logs capture all potentially relevant data, but the attempt to do this usually results in logging too much information. Logging libraries may have evolved to deal with the problem of decreased performance that this approach causes but too many log messages are difficult to read and identify a problem when it occurs.
Therefore, you need to reduce the overall logging footprint by only logging important information that cut across your system. In an attempt to log as much as possible, you may also commit the mistake of logging sensitive information which is not only insecure but also illegal.
Rigid logging configuration
Your first go-to point when it comes to logging may be to use Python standard library logging module just like many programmers. The configuration may look overwhelming at first, but you will overcome that after going through the ultimate guide to Python logging.
Logging in a plaintext format has been overtaken by open-file standard formats like JSON and YAML since they are not only readable but also easy to write and parse.
Incorrect log levels
Most logging frameworks identify logging levels with FATAL, ERROR, WARN, INFO, DEBUG and TRACE in the order of the most to the least severe error. It is therefore important to choose an adequate level so that you do not miss important events or receive too much unnecessary data. It will not make sense to get a warning for a problem that is critical and vice versa.
Using one log file
If you have the habit of using a single log file for your application, the things might not go well because a large file is a nightmare to work with. It is therefore good to create a new log file on a daily basis to be placed in specific folders.
Logging is a significant part of application technology, and it is important that you follow the best practices. Logging the most important information across your entire system, putting your log messages in context, avoiding excessive logging and use of the correct logging levels are some of the practices that you should always adhere to. As you log freely but wisely, don’t forget to use proper tools so that you can get good insights out of your log files.