一、环境准备
pip install concurrent-log-handler
pip install pyyaml
二、配置示例
- config/logging_config.py
import logging
import logging.config
import os
import yaml_logger_initialized = Falsedef setup_logger(config_path="../config/logging_config.yaml"):global _logger_initializedif _logger_initialized:returnprint(f"正在尝试加载日志配置文件:{os.path.abspath(config_path)}") if not os.path.exists(config_path):raise FileNotFoundError(f"日志配置文件未找到: {config_path}")with open(config_path, "r", encoding="utf-8") as f:config = yaml.safe_load(f)handlers = config.get('handlers', {})for handler in handlers.values():if 'filename' in handler:log_file = handler['filename']log_dir = os.path.dirname(log_file)if log_dir and not os.path.exists(log_dir):os.makedirs(log_dir, exist_ok=True)print(f"已创建日志目录:{log_dir}")logging.config.dictConfig(config)_logger_initialized = True
- config/logging_config.yaml
version: 1
disable_existing_loggers: Falseformatters:simple:format: "%(asctime)s - %(levelname)s - %(name)s.%(funcName)s:%(lineno)d - %(message)s"handlers:console:class: logging.StreamHandlerlevel: INFOformatter: simplestream: ext://sys.stdoutinfo_file:class: concurrent_log_handler.ConcurrentTimedRotatingFileHandlerlevel: INFOformatter: simplefilename: ../logs/info/log_info.logwhen: midnightbackupCount: 7maxBytes: 104857600 encoding: utf8error_file:class: concurrent_log_handler.ConcurrentTimedRotatingFileHandlerlevel: ERRORformatter: simplefilename: ../logs/error/log_error.logwhen: midnightbackupCount: 7maxBytes: 104857600 encoding: utf8loggers:root: level: DEBUGhandlers: [console, info_file, error_file]'spiders.js_spider':level: INFO
三、目录结构及结果展示
- 执行时需要在主函数main方法中执行setup_logger()初始化日志
