YouTip LogoYouTip

Python Logging

## Python3.x Python logging Module In programming, logging is a very important tool that helps us track program execution, debug errors, and record important information. Python provides a built-in `logging` module specifically for handling logging tasks. Compared with simple `print` statements, the `logging` module is more flexible and powerful, and can meet logging needs in different scenarios. ### Why Use the logging Module? 1. **Flexibility**: The `logging` module allows you to set log levels, formats, and output destinations as needed. 2. **Extensibility**: You can easily output logs to different targets such as files, consoles, networks, etc. 3. **Structured Logging**: The `logging` module supports structured logging, which facilitates subsequent analysis and processing. 4. **Performance Optimization**: Compared with `print`, the `logging` module is optimized for performance and is suitable for use in production environments. * * * ### 1. Import the logging Module First, we need to import the `logging` module: ## Example import logging ### 2. Configure Log Level Log levels are used to control the verbosity of logs. The `logging` module provides the following log levels: * **DEBUG**: Detailed debug information, usually used during development. * **INFO**: Information when the program runs normally. * **WARNING**: Indicates potential problems, but the program can still run normally. * **ERROR**: Indicates errors in the program, causing some functions to not work properly. * **CRITICAL**: Indicates serious errors that may cause the program to crash. You can set the log level with the following code: ## Example logging.basicConfig(level=logging.DEBUG) ### 3. Record Logs After setting the log level, you can use the following methods to record logs: ## Example logging.debug("This is a debug message") logging.info("This is an info message") logging.warning("This is a warning message") logging.error("This is an error message") logging.critical("This is a critical error message") ### 4. Log Output Format You can customize the log output format through the `basicConfig` method. For example: ## Example logging.basicConfig( level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S" ) ### 5. Output 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 logging.basicConfig( level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", filename="app.log" ) * * * ### Advanced Usage of the logging Module ### 1. Using Multiple Loggers In large projects, you may need to create separate loggers for different modules or components. This can be achieved as follows: ## Example logger =logging.getLogger("my_logger") logger.setLevel(logging.DEBUG) # Create file handler file_handler =logging.FileHandler("my_logger.log") file_handler.setLevel(logging.DEBUG) # Create console handler console_handler =logging.StreamHandler() console_handler.setLevel(logging.INFO) # Set log format formatter=logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") file_handler.setFormatter(formatter) console_handler.setFormatter(formatter) # Add handlers to logger logger.addHandler(file_handler) logger.addHandler(console_handler) # Record logs logger.debug("This is a debug message") logger.info("This is an info message") ### 2. Log Filters You can use filters to control which logs need to be recorded. For example: ## Example class MyFilter(logging.Filter): def filter(self, record): return record.levelno==logging.ERROR logger.addFilter(MyFilter()) ### 3. Log Rotation When log files become too large, you can use `RotatingFileHandler` or `TimedRotatingFileHandler` to implement log rotation: ## Example from logging.handlers import RotatingFileHandler handler = RotatingFileHandler("app.log", maxBytes=1024, backupCount=3) logger.addHandler(handler) * * * ### Common Attributes and Methods of the logging Module ### 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 (such as file, console, etc.) | `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 finer control over log recording | `filter = logging.Filter("module.name")` | ### 2. Common Methods of Logger Object | Method | Description | Example | | --- | --- | --- | | **`logger.setLevel(level)`** | Set log level (e.g., `logging.DEBUG`, `logging.INFO`) | `logger.setLevel(logging.DEBUG)` | | **`logger.debug(msg)`** | Record DEBUG level log | `logger.debug("Debug info")` | | **`logger.info(msg)`** | Record INFO level log | `logger.info("Program started")` | | **`logger.warning(msg)`** | Record WARNING level log | `logger.warning("Disk space insufficient")` | | **`logger.error(msg)`** | Record ERROR level log | `logger.error("Operation failed")` | | **`logger.critical(msg)`** | Record 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 | Handler Type | Description | Example | | --- | --- | --- | | **`StreamHandler`** | Output to stream (such as console) | `handler = logging.StreamHandler()` | | **`FileHandler`** | Output to 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", "from@example.com", "to@example.com", "Error Log")` | ### 4. Log Levels (Constants) | Level | Value | Description | | --- | --- | --- | | **`CRITICAL`** | 50 | Critical error, program may not be able to continue running | | **`ERROR`** | 40 | Error, but program can still run | | **`WARNING`** | 30 | Warning message (default level) | | **`INFO`** | 20 | Program running information | | **`DEBUG`** | 10 | Debug information | | **`NOTSET`** | 0 | Inherit parent logger's level | ### 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` | Filename that generated the log | `app.py` | | `%(lineno)d` | Line number that generated the log | `42` | | `%(funcName)s` | Function name that generated the log | `main` | ### 6. Quick Configuration Methods | Method | Description | Example | | --- | --- | --- | | **`logging.basicConfig()`** | One-click configuration of log level, handler, and format (usually called at program entry) | `logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s')` | **Common Parameters**: * `level`: Set root logger level * `filename`: Output
← Python RePython Queue β†’