Files
arlo/log/log_manager.py
konjacpotato ca39d1f891
All checks were successful
Gitea Actions Demo / deploy (push) Successful in 30s
调整
2025-11-14 21:58:36 +08:00

77 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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}')