Recommended Practices for Python Logging for Python Developers
Python is no different from the majority of logging tools in that it offers several logging levels. You can see three of them in the preceding example: INFO, DEBUG, and WARNING.
These are just a few of the logging levels the logging module supports. But in this blog, we will be going through these and many more logging levels, that too in great detail with code examples.
So stick to your seat and let’s begin with understanding what are logging levels in the brief section below.
What Are Logging Levels
What are logging levels, though? Simply put, they are labels that you attach to your log entries. After that, you can search and filter through your log entries using these labels.
You may adjust the granularity of the data in your logs by using different logging levels. Only events that fall within that level or higher will be recorded when you define a log level using the standard logging library.
Basic Logging Configuration
Loggers, handlers, and formatters are the logging module’s primary constituents. When you wish to record a message in your application code, you call loggers.
On certain platforms, handlers are also known as targets, appenders, or writers. These are the elements that write messages to their target efficiently.
For example, you could have handlers for logging to syslog, another file, and several additional files. As its name suggests, formatters are in charge of formatting the format of log messages.
The majority of configurations include a formatter and a handler. The API has a logging function called basicConfig (). Look at the following example:
1 import logging 2 logging.basicConfig(level=logging.INFO) 3 logging.info(‘This message will be logged’) 4 logging.debug(‘This message will not be logged’) |
The logger is configured with the INFO level in the aforementioned example using the basicConfig() method, therefore only events with a level of INFO or above will be logged.
Now, look at the following example.
1 2 import logging 3 4 5 logging.basicConfig(filename=’myfirstlog.log’, 6 level=logging.DEBUG, 7 format=’%(asctime)s | %(name)s | %(levelname)s | %(message)s’) 8 9 10 logging.info(‘This message will be logged’) 11 logging.debug(‘This message will not be logged’) 12 |
We are setting more complex formatting in this instance. Testing log formatting! would produce the following output if it were to be logged at the WARN level:
2023-02-23 16:42:09,035 | root | INFO | This message will be logged 2023-02-23 16:42:09,035 | root | DEBUG | This message will not be logged |
Python Logging Best Practices
These are some best practices for Python logging to guide you.
1: Use the Python Standard Logging Module
In keeping with tried-and-true approaches, the first “do” on our list shouldn’t come as a surprise: stick with the traditional logging module. It is flexible and simple to use thanks to the design. As you have already seen, the logging module makes it simple for you to define handlers and formatters and combine them to produce effective results.
2: Use the Correct Levels When Logging
Choosing which level to give each event may be challenging. The Python logging module, thankfully, offers less leveling than other logging libraries. This removes some potential uncertainty, which simplifies things. The general guidelines for Python levels are as follows.
Debug
You should use this level for debugging purposes in development.
Info
You should use this level when something interesting—but expected—happens (e.g., a user starts a new project in a project management application).
Warning
You should use this level when something unexpected or unusual happens. It’s not an error, but you should pay attention to it.
Error
This level is for things that go wrong but are usually recoverable (e.g., internal exceptions you can handle or APIs returning error results).
Critical
You should use this level in a doomsday scenario. The application is unusable. At this level, someone should be woken up at 2 a.m.
3: Include a Timestamp for Each Log Entry
Only slightly better than not knowing anything at all is knowing that something occurred but not knowing when it occurred.
To make the life of those who utilize logs for troubleshooting easier, be sure to include a timestamp in each of your log entries.
Developers are also given the opportunity to examine the log entries in order to gather knowledge about user activity.
4: Adopt the ISO-8601 Format for Timestamps
Log entries must have timestamps. We unfortunately come up with a number of contradictory formats because no one can agree on the ideal approach to depict moments in time.
If you don’t intend to submit your application abroad, using the format that is commonly accepted in your nation may seem like the best option. And nothing could be further from the truth than this.
You may avoid issues by simply using a standard format for your timestamps because third-party libraries and utilities will already expect this format.
This format is a recognized standard and is known as ISO-8601. It is a universally recognized protocol for exchanging time- and date-related information. Here is an illustration of a timestamp in ISO-8601 format.
2023-02-23 16:42:09,035 |
Here is a simple illustration of how to set up the formatting to accept ISO-8601 timestamps:
1 import logging 2 logging.basicConfig(format=‘%(asctime)s %(message)s’) 3 logging.info(‘Example of logging with ISO-8601 timestamp’) |
5: Use the RotatingFileHandler Class
Rotating logs is a best practice for logging in general, regardless of the language. To avoid full discs, this technique automatically archives, compresses, or deletes outdated log files.
Thankfully, Python allows you to avoid manually implementing this. Use the RotatingFileHandler class rather than the default FileHandler one instead.
Conclusion
One of the most widely used languages is Python. It provides simplicity, adaptability, and a substantial ecology of outside tools. Python’s flexibility allows it to handle a wide range of use cases, including web applications, data science libraries, SysAdmin scripts, and many other kinds of software.
All software professionals can benefit from reviewing Python logging and highlighting best practices because logging is crucial for the majority of applications, especially for those who are just getting started.