import logging.config import sys from config import config """ Usage: 1 code from log.log_manager import logger logger.info("Starting Jarvas") 2 app start python demo.py --logconfig=log_prod.config 当前目录下的log_prod.config是一份参考配置 """ # default logging config for development LOG_DEV_CONFIG = { "version": 1, "disable_existing_loggers": False, "loggers": { "root": { "level": "INFO", "handlers": ["consoleHandler"] } }, "handlers": { "consoleHandler": { "class": "logging.StreamHandler", "level": "INFO", "formatter": "verbose", "stream": sys.stdout } }, "formatters": { "verbose": { "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s", "datefmt": "%Y-%m-%d %H:%M:%S" } } } log_config_message = "" # 获取命令行参数 args = sys.argv # 查找包含 'logconfig' 的参数 logconfig_param = next((arg for arg in args if '--logconfig' in arg), None) logconfig_value = None if logconfig_param: # 如果找到了 logconfig 参数,提取其值 _, logconfig_value = logconfig_param.split('=') # 以 '=' 分割 log_config_message = f"--logconfig value: {logconfig_value}" else: log_config_message = "没有找到 --logconfig 参数,使用默认log配置" if logconfig_value: # 使用入参日志配置文件 logging.config.fileConfig(logconfig_value) else: # 使用默认日志配置 logging.config.dictConfig(LOG_DEV_CONFIG) logger = logging.getLogger('root') # 打印日志配置信息 logger.info(log_config_message) def log(message: str) -> None: """Helper wrapper to log a message prefixed with the scheduler name. Kept small and typed to be a safe, low-risk refactor: it unifies how `config` is imported across the codebase (other modules use `from config import config`). """ logger.info(f'{config.scheduler_name} {message}')