Python logging Module
In programming, logging is a very important tool that helps us track program execution status, debug errors, and record important information.
Python provides a built-in logging module specifically designed to handle logging tasks. Compared to simple print statements, the logging module is more flexible and powerful, and can meet logging needs in various scenarios.
Why Use the logging Module?
Section titled “Why Use the logging Module?”- Flexibility: The
loggingmodule allows you to set the log level, format, and output destination as needed. - Extensibility: You can easily output logs to different targets such as files, consoles, and networks.
- Structured Logging: The
loggingmodule supports structured logging, facilitating subsequent analysis and processing. - Performance Optimization: Compared to
print, theloggingmodule is optimized for performance, making it suitable for production environments.
Basic Usage of the logging Module
Section titled “Basic Usage of the logging Module”1. Importing the logging Module
Section titled “1. Importing the logging Module”First, we need to import the logging module:
Example
Section titled “Example”2. Configuring Log Levels
Section titled “2. Configuring Log Levels”Log levels are used to control the verbosity of logs. The logging module provides the following log levels:
- DEBUG: Detailed debugging information, typically used during development.
- INFO: Information about normal program execution.
- WARNING: Indicates potential problems, but the program can still run normally.
- ERROR: Indicates errors in the program that prevent certain functions from working properly.
- CRITICAL: Indicates serious errors that may cause the program to crash.
You can set the log level with the following code:
Example
Section titled “Example”3. Recording Logs
Section titled “3. Recording Logs”After setting the log level, you can use the following methods to record logs:
Example
Section titled “Example”4. Log Output Format
Section titled “4. Log Output Format”You can customize the log output format through the basicConfig method. For example:
Example
Section titled “Example”5. Outputting Logs to a File
Section titled “5. Outputting Logs to a File”By default, logs are output to the console. If you want to save logs to a file, you can configure it like this:
Example
Section titled “Example”Advanced Usage of the logging Module
Section titled “Advanced Usage of the logging Module”1. Using Multiple Loggers
Section titled “1. Using Multiple Loggers”In large projects, you may need to create independent loggers for different modules or components. You can achieve this in the following way:
Example
Section titled “Example”2. Log Filters
Section titled “2. Log Filters”You can use filters to control which logs need to be recorded. For example:
Example
Section titled “Example”3. Log Rotation
Section titled “3. Log Rotation”When log files become too large, you can use RotatingFileHandler or TimedRotatingFileHandler to implement log rotation:
Example
Section titled “Example”Common Attributes and Methods of the logging Module
Section titled “Common Attributes and Methods of the logging Module”1. Core Classes
Section titled “1. Core Classes”| Class | Description | Example |
|---|---|---|
logging.Logger |
Logger, used to emit log messages (obtained via logging.getLogger(name)) |
logger = logging.getLogger("my_logger") |
logging.Handler |
Handler, determines where logs are output (e.g., file, console) | handler = logging.FileHandler("app.log") |
logging.Formatter |
Formatter, controls the format of log output | formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') |
logging.Filter |
Filter, used for more fine-grained control of log recording | filter = logging.Filter("module.name") |
2. Common Methods of the Logger Object
Section titled “2. Common Methods of the Logger Object”| Method | Description | Example |
|---|---|---|
logger.setLevel(level) |
Set the log level (e.g., logging.DEBUG, logging.INFO) |
logger.setLevel(logging.DEBUG) |
logger.debug(msg) |
Record a DEBUG level log | logger.debug("Debug message") |
logger.info(msg) |
Record an INFO level log | logger.info("Program started") |
logger.warning(msg) |
Record a WARNING level log | logger.warning("Low disk space") |
logger.error(msg) |
Record an ERROR level log | logger.error("Operation failed") |
logger.critical(msg) |
Record a CRITICAL level log | logger.critical("System crash") |
logger.addHandler(handler) |
Add a handler | logger.addHandler(handler) |
logger.addFilter(filter) |
Add a filter | logger.addFilter(filter) |
3. Common Handler Types
Section titled “3. Common Handler Types”| Handler Type | Description | Example |
|---|---|---|
StreamHandler |
Output to a stream (e.g., console) | handler = logging.StreamHandler() |
FileHandler |
Output to a file | handler = logging.FileHandler("app.log") |
RotatingFileHandler |
Split logs by file size | handler = logging.RotatingFileHandler("app.log", maxBytes=1e6, backupCount=3) |
TimedRotatingFileHandler |
Split logs by time | handler = logging.TimedRotatingFileHandler("app.log", when="midnight") |
SMTPHandler |
Send logs via email | handler = logging.SMTPHandler("mail.example.com", "[email protected]", "[email protected]", "Error Log") |
4. Log Levels (Constants)
Section titled “4. Log Levels (Constants)”| Level | Value | Description |
|---|---|---|
CRITICAL |
50 | Serious error, program may not continue running |
ERROR |
40 | Error, but program can still run |
WARNING |
30 | Warning message (default level) |
INFO |
20 | Program execution information |
DEBUG |
10 | Debugging information |
NOTSET |
0 | Inherit the level of the parent logger |
5. Common Formatter Format Fields
Section titled “5. Common Formatter Format Fields”| Field | Description | Example Output |
|---|---|---|
%(asctime)s |
Log creation time | 2023-01-01 12:00:00,123 |
%(levelname)s |
Log level name | INFO |
%(message)s |
Log message content | Program started successfully |
%(name)s |
Logger name | my_logger |
%(filename)s |
File name where log was generated | app.py |
%(lineno)d |
Line number where log was generated | 42 |
%(funcName)s |
Function name where log was generated | main |
6. Quick Configuration Method
Section titled “6. Quick Configuration Method”| Method | Description | Example |
|---|---|---|
logging.basicConfig() |
One-click configuration of log level, handler, and format (usually called at the program entry point) | logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s') |
Common Parameters:
-
level: Set the root logger level -
filename: Output to a file -
filemode: File mode (e.g.,'w'for overwrite) -
format: Format string -
datefmt: Date format (e.g.,"%Y-%m-%d %H:%M:%S")
Examples
Section titled “Examples”1. Basic Configuration
Example
Section titled “Example”2. Complex Configuration with Multiple Handlers