These examples are just reminders/demonstrations to help me get started on any given python script.
For a more detailed description of Python logging refer to the official manual. There is a useful diagram outlining the flow of log event information in loggers and handlers at: https://docs.python.org/3.10/howto/logging.html#logging-flow. And don't ignore the logging cookbook if you have the urge to do your own thing https://docs.python.org/3/howto/logging-cookbook.html
- Or a quick overview: "Logging in Python: A Developer’s Guide." By Bala Priya C., 2022-07-19 https://blog.sentry.io/2022/07/19/logging-in-python-a-developers-guide
- Or for troubleshooting see: https://github.com/brandon-rhodes/logging_tree
- or a short video lesson at: "Python Logging: How to Write Logs Like a Pro!" with its supporting code at: https://github.com/ArjanCodes/2023-logging
- The old print way:
def log(loglevel, component, message):
print(f"{datetime.now(timezone('UCT'))} \
[{loglevel}] {component}: {message}", file=sys.stderr)Then log something with:
log("INFO", __file__, f"Using important_var: {important_var}")-
The standard python logging module:
trylogging/LogTestLogging.py
Usage:
from LogTestLogging import init_logging
logger = init_logging()
logger.info('info testing message') -
The loguru module -- because it was so easy to configure:
tryloguru/LogTestLoGuru.py
Usage:
from LogTestLoGuru import init_logging
logger = init_logging()
logger.info('info testing message')- See "A Complete Guide to Logging in Python with Loguru" for an excellent overview and a detailed example describing how to integrate it into a typical web application.
-
requestsDebugging: A useful approach to requests debug logging especially valuable when attempting to deal with a poorly documented or otherwise opaque API. This is well explained by Ben Hoey at bhoey.com.
-
The next...
None. These are not modules, just little code reminders.
That said, tryloguru/LogTestLoGuru.py requires pip3 install loguru --user.
See some ideas at: https://github.com/mccright/rand-notes/blob/master/Application-Logging.md
- Try 'minilog' a minimalistic logging wrapper for Python.
https://github.com/jacebrowning/minilog - Try 'Python Quick Logging | QLogging', colored Python logging based on Python the logging package.
https://github.com/sinkingtitanic/qlogging - This utility approach: https://github.com/karldoenitz/PythonUtils/tree/master/logger
- Minimal approach: https://github.com/cherkavi/python-utilities/blob/master/logging/log-example.py
- If all you want is quick, easy console logging, look at https://github.com/apparebit/konsole
- "structlog makes logging in Python faster, less painful, and more powerful by adding structure to your log entries. It has been successfully used in production at every scale since 2013" https://github.com/hynek/structlog
- "Python logging: do's and don'ts" https://www.palkeo.com/en/blog/python-logging.html
- "Logging in Python like a PRO" https://blog.guilatrova.dev/how-to-log-in-python-like-a-pro/
- "The Ins and Outs of Logging in Python, Part 1" https://monadical.com/posts/ins-and-outs-of-logging-in-python-part-one.html#
- "Logging practices I follow" https://www.16elt.com/2023/01/06/logging-practices-I-follow/
- "How to Pickle and Unpickle Objects in Python." https://stackabuse.com/how-to-pickle-and-unpickle-objects-in-python/