


Mastering Python Logging: From Basics to Advanced Techniques
Logging in Python is more than just debugging—it's about tracking, monitoring, and understanding your application’s behavior. Whether you're a beginner or an experienced developer, this guide covers all aspects of logging, from basic setups to advanced techniques.
Introduction
What is logging?
Logging is a mechanism to record and track events during the execution of a program, helping developers debug, monitor, and analyze their applications effectively.
Why is logging essential?
Unlike print, logging offers flexibility, scalability, and configurability, making it a robust choice for both small scripts and large applications.
What this blog covers
Setting up basic logging
Writing logs to files
Creating custom loggers
Formatting log outputs
Advanced techniques like log rotation and configurations
Best practices and common mistakes
What is Logging in Python?
Introduce the logging module.
Explain logging levels:
DEBUG: Detailed information for diagnosing issues.
INFO: Confirmation that the program is working as expected.
WARNING: Something unexpected happened, but the program can still run.
ERROR: A problem caused an operation to fail.
CRITICAL: A serious error that might stop the program.
Setting Up Basic Logging
Introduce logging.basicConfig.
Provide a simple example:
import logging # Basic configuration logging.basicConfig(level=logging.INFO) # Logging messages logging.debug("Debug message") logging.info("Info message") logging.warning("Warning message") logging.error("Error message") logging.critical("Critical message")
Output
By default, only messages at WARNING level or above are displayed on the console. The above example produces:
WARNING:root:Warning message
ERROR:root:Error message
CRITICAL:root:Critical message
Writing Logs to a File
logging.basicConfig(filename="app.log", level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s") logging.info("This will be written to a file.")
Explain common parameters in basicConfig:
filename: Specifies the log file.
filemode: 'w' to overwrite or 'a' to append.
format: Customizes log message structure.
Creating Custom Loggers
Why use custom loggers? For modular and more controlled logging.
Example:
import logging # Create a custom logger logger = logging.getLogger("my_logger") logger.setLevel(logging.DEBUG) # Create handlers console_handler = logging.StreamHandler() file_handler = logging.FileHandler("custom.log") # Set levels for handlers console_handler.setLevel(logging.INFO) file_handler.setLevel(logging.ERROR) # Create formatters and add them to handlers formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s") console_handler.setFormatter(formatter) file_handler.setFormatter(formatter) # Add handlers to the logger logger.addHandler(console_handler) logger.addHandler(file_handler) # Log messages logger.info("This is an info message.") logger.error("This is an error message.")
** Formatting Logs**
Explain log record attributes:
%(asctime)s: Timestamp.
%(levelname)s: Level of the log message.
%(message)s: The actual log message.
%(name)s: Logger's name.
Advanced formatting:
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.DEBUG)
Log Rotation
Introduce RotatingFileHandler for managing log file size.
Example:
from logging.handlers import RotatingFileHandler # Create a logger logger = logging.getLogger("rotating_logger") logger.setLevel(logging.DEBUG) # Create a rotating file handler handler = RotatingFileHandler("app.log", maxBytes=2000, backupCount=3) formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # Log messages for i in range(100): logger.info(f"Message {i}")
Using logging.config for Complex Configurations
Show how to use a configuration dictionary:
import logging # Basic configuration logging.basicConfig(level=logging.INFO) # Logging messages logging.debug("Debug message") logging.info("Info message") logging.warning("Warning message") logging.error("Error message") logging.critical("Critical message")
Best Practices for Logging
Use meaningful log messages.
Avoid sensitive data in logs.
Use DEBUG level in development and higher levels in production.
Rotate log files to prevent storage issues.
Use unique logger names for different modules.
Common Mistakes
Overusing DEBUG in production.
Forgetting to close file handlers.
Not using a separate log file for errors.
Advanced Topics
Asynchronous Logging
For high-performance applications, use QueueHandler to offload logging tasks asynchronously.
Structured Logging
Log messages as JSON to make them machine-readable, especially for systems like ELK Stack.
Third-Party Libraries
Explore tools like loguru for simpler and more powerful logging.
Conclusion
Logging is not just about debugging—it's about understanding your application. By mastering Python's logging module, you can ensure your projects are robust, maintainable, and easy to debug.
Have questions or suggestions? Share your thoughts in the comments below!
The above is the detailed content of Mastering Python Logging: From Basics to Advanced Techniques. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

Fastapi ...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.
