Overview Effective logging turns a black-box application into a transparent system. While many developers rely on print statements during development, Python's logging package provides a robust framework for tracking events, diagnosing bugs, and monitoring production health. It allows you to categorize messages by severity and direct them to various outputs without changing your core logic. Prerequisites To follow this guide, you should have a basic understanding of Python syntax and modular programming. Knowledge of file handling and external API integration is helpful but not required. Key Libraries & Tools * **logging**: The built-in Python module for event tracking. * **PaperTrail**: A cloud-based log management service for centralized visualization. * **syslog**: A standard for message logging used to send data to external handlers. Code Walkthrough Basic Configuration The simplest way to start is using `basicConfig`. This sets the global threshold for what messages actually get recorded. ```python import logging Set the threshold to INFO logging.basicConfig(level=logging.INFO) logging.debug("This won't show") logging.info("This will show") ``` Formatting and File Output You can customize the look of your logs and redirect them from the console to a file using the `format` and `filename` arguments. ```python logging.basicConfig( level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filename='app.log' ) ``` Using Handlers for External Services For production, use Logger objects and handlers to send data to services like PaperTrail. ```python from logging.handlers import SysLogHandler logger = logging.getLogger("MyApp") handler = SysLogHandler(address=('your-host.papertrailapp.com', 12345)) logger.addHandler(handler) logger.warning("External log message") ``` Syntax Notes Python uses five standard levels: `DEBUG`, `INFO`, `WARNING`, `ERROR`, and `CRITICAL`. The default level is `WARNING`. Use the `%(name)s` placeholder in formats to identify which module triggered the log. Practical Examples Commercial platforms use logging to detect hacking attempts by monitoring failed login frequency. Developers also use it to trace back the exact state of a system before a crash occurs in a customer environment. Tips & Gotchas Never log sensitive data. Twitter and Cam4 suffered massive data leaks because they stored unhashed passwords or PII in logs. Treat your log files as if they are publicly accessible; avoid logging credit card numbers or email addresses entirely.
logging
Products
- Feb 3, 2023
- Jan 27, 2023