The “logging_handler” provides a quick and easy way to create a logging object in Python. The Python logging is an incredibly powerful tool, however the options are extensive and the defaults I find to be less than adaquate. The the code in this library I have been using for years on every Python script I have written.
Module Homepage: Python logging_handler | |
Git: https://github.com/LearningToPi/logging_handler | |
PyPi: https://pypi.org/project/logging-handler/ |
Install logging_handler
The latest release can be downloaded from the Git Repo (https://github.com/LearningToPi/logging_handler/releases) or from PyPi (https://pypi.org/project/logging-handler/#files). I keep releases in both locations up to date. You can also install directly using Pip:
pip3 install -U logging_handler
(NOTE: -U will upgrade if a newer version is available)
Why I Created logging_handler
I always make sure I have a good reason for publishing code that I use. This particular library has a single function “create_logger’ that creates a python logging object using some standard output that I find far more useful than the default logging settings. You may ask, why not just use print()? I frequently use theaded or multiprocess scripts, and have found the python logging to be incredibly powerful, but somewhat difficult to manage.
Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.warning('this is a warning!')
WARNING:root:this is a warning!
>>> logging.info('just useful info')
>>> logging.debug('debug message!')
>>>
With the default logging configuraiton shown above, we can see that only the “warning” message was printed on the screen. By default only warning and above messages are output on the screen. Also, you’ll notice that the message is tagged as “root”, and there is no timestamp! Reviewing logs without any timestamp I’ve found to be less than useful.
This was the initial driver to make adding timestamps quick and easy. Once I had timestamps, adding logging to the console as well as to file (using different logging levels) was the next add. I also found cases with Raspberry Pi’s that logging to the local syslog facility was beneficial and would allow me to correlate errors with system events (and also let me aggregate the script messages on my central syslog server). The last addition was to create logs based on the date and handle rotation (this was specifically added for a script that ran daily that had a large amount of output that I didn’t want to store forever).
How It Works
Using the create_logger function from logging_handler is quick and simple. Simply import the function (you may also the level names DEBUG, INFO, WARNING, ERROR, and CRITICAL as well, but this is optional). The function will configure the logger with console / file / syslog logging and return it. The returned logger is simply a Python logging object that can be used just like the example above.
>>> from logging_handler import create_logger
>>> logger = create_logger(console_level='DEBUG')
>>> logger.warning('this is a warning!')
2022-11-10 13:46:43,371 - root - WARNING - this is a warning!
>>> logger.info('just useful info')
2022-11-10 13:46:52,419 - root - INFO - just useful info
>>> logger.debug('debug message!')
2022-11-10 13:47:10,435 - root - DEBUG - debug message!
We can also get more complex and add logging to file and syslog:
>>> from logging_handler import create_logger
>>> logger = create_logger(console_level='DEBUG', log_file='test.log', file_level='INFO', syslog=True)
>>>
>>> logger.warning('this is a warning!')
2022-11-10 13:49:41,264 - root - WARNING - this is a warning!
>>> logger.info('just useful info')
2022-11-10 13:49:47,901 - root - INFO - just useful info
>>> logger.debug('debug message!')
2022-11-10 13:49:53,290 - root - DEBUG - debug message!
>>> quit()
The log file and syslog will now contain the messages that were at or above the file level (syslog uses the same setting as the file_level):
(venv) pi@raspberrypi:~ $ cat test.log
2022-11-10 13:49:41,264 - root - WARNING - this is a warning!
2022-11-10 13:49:47,901 - root - INFO - just useful info
(venv) pi@raspberrypi:~ $ sudo tail /var/log/syslog
<...output omitted...>
Nov 10 13:49:41 raspberrypi [2686]: WARNING: this is a warning!
Nov 10 13:49:47 raspberrypi [2686]: INFO: just useful info
(venv) pi@raspberrypi:~ $
For a breakdown of all the functions available in the module, check out the git repo (https://github.com/LearningToPi/logging_handler) for the latest documentation.
Happy coding!