diff --git a/docs/BasisModule/Trace/Debug.md b/docs/BasisModule/Trace/Debug.md index 6e0f5680..86eec3de 100644 --- a/docs/BasisModule/Trace/Debug.md +++ b/docs/BasisModule/Trace/Debug.md @@ -61,7 +61,7 @@ os.Setenv("APPBUILDER_LOGLEVEL", "/tmp/appbuilder.log") 参数说明: - rolling (bool, optional): 是否启用滚动日志. 默认为True. - update_interval (int, optional): 更新日志文件的间隔时间. 默认为1. -- update_time (str, optional): 更新日志文件的时间间隔单位. 默认为空字符串. +- update_time (str, optional): 更新日志文件的时间间隔单位. 默认为'midnight',每日凌晨更新. - 可选值: - 'S' - Seconds - 'M' - Minutes @@ -80,8 +80,8 @@ appbuilder.logger.setLoglevel("DEBUG") 这里设置: rolling=Ture - 启用滚动日志(运行此段代码,默认使用滚动日志) filename="appbuilder.log" - 此优先级最高会覆盖之前的设置,若未传参则使用之前已经设置的日志文件,若之前未设置则使用默认的"tmp.log"日志文件 - update_interval = 30 - 更新日志文件的间隔数量级 - update_time = 'S' - 更新日志文件的间隔单位为秒级 + update_interval = 1 - 更新日志文件的间隔数量级 + update_time = 'midnight' - 更新日志文件的间隔单位为秒级,每日凌晨滚动日志 """ -appbuilder.logger.setLogConfig(filename="appbuilder.log",update_interval=30, update_time='S') +appbuilder.logger.setLogConfig(filename="appbuilder.log",update_interval=1, update_time='midnight') ``` \ No newline at end of file diff --git a/python/utils/logger_util.py b/python/utils/logger_util.py index 7536dfce..4003072b 100644 --- a/python/utils/logger_util.py +++ b/python/utils/logger_util.py @@ -62,6 +62,17 @@ 'encoding': 'utf-8', } +TIME_HANDLERS_FILE_ERROR = { + 'class': 'logging.handlers.TimedRotatingFileHandler', + 'formatter': 'standard', + 'level': 'ERROR', + 'filename': "", + 'when': 'midnight', # 可选项: 'S', 'M', 'H', 'D', 'W0'-'W6', 'midnight' + 'interval': 1, # 每1天滚动一次 + 'backupCount': 5, # 保留5个备份 + 'encoding': 'utf-8', +} + SIMPLE_HANDLERS_FILE = { "level": "INFO", "class": "logging.FileHandler", @@ -69,6 +80,12 @@ "formatter": "standard", } +SIMPLE_HANDLERS_FILE_ERROR = { + "level": "ERROR", + "class": "logging.FileHandler", + "filename": "", + "formatter": "standard", +} class LoggerWithLoggerId(logging.LoggerAdapter): """ @@ -81,12 +98,15 @@ def __init__(self, logger, extra, loglevel): log_file = os.environ.get("APPBUILDER_LOGFILE", "") if log_file: LOGGING_CONFIG["loggers"]["appbuilder"]["handler"] = ["console"] # 默认使用console - SIMPLE_HANDLERS_FILE["filename"] = log_file - TIME_HANDLERS_FILE['filename'] = log_file + SIMPLE_HANDLERS_FILE["level"] = loglevel TIME_HANDLERS_FILE['level'] = loglevel + SIMPLE_HANDLERS_FILE["filename"] = log_file + SIMPLE_HANDLERS_FILE_ERROR["filename"] = f"error.{log_file}" LOGGING_CONFIG["handlers"]["file"] = SIMPLE_HANDLERS_FILE + LOGGING_CONFIG["handlers"]["error_file"] = SIMPLE_HANDLERS_FILE_ERROR LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("file") + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("error_file") LOGGING_CONFIG['handlers']['console']['level'] = loglevel LOGGING_CONFIG['loggers']['appbuilder']['level'] = loglevel logging.config.dictConfig(LOGGING_CONFIG) @@ -125,13 +145,15 @@ def setLogConfig( self, rolling:bool=True, update_interval:int=1, - update_time:str='', + update_time:str='midnight', backup_count:int=0, filename:str='' ): # 配置控制台输出 if "file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("file") + if "error_file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("error_file") # 确定备份数量 if backup_count <= 0 or not isinstance(backup_count, int): @@ -149,37 +171,69 @@ def setLogConfig( # 设置filename if not filename: + filename = (SIMPLE_HANDLERS_FILE.get("filename") or TIME_HANDLERS_FILE.get("filename") or "tmp.log") + + # 创建处理器 if rolling: if update_time: TIME_HANDLERS_FILE['when'] = update_time + TIME_HANDLERS_FILE_ERROR['when'] = update_time TIME_HANDLERS_FILE['interval'] = update_interval + TIME_HANDLERS_FILE_ERROR['interval'] = update_interval TIME_HANDLERS_FILE['backupCount'] = backup_count + TIME_HANDLERS_FILE_ERROR['backupCount'] = backup_count TIME_HANDLERS_FILE['filename'] = filename + TIME_HANDLERS_FILE_ERROR['filename'] = f"error.{filename}" if 'file' in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("file") + if 'error_file' in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("error_file") if not "timed_file" in LOGGING_CONFIG["handlers"]: LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append('timed_file') + if not "error_timed_file" in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append('error_timed_file') LOGGING_CONFIG["handlers"]["timed_file"] = TIME_HANDLERS_FILE + LOGGING_CONFIG["handlers"]["error_timed_file"] = TIME_HANDLERS_FILE_ERROR LOGGING_CONFIG["handlers"]["timed_file"]["level"] = LOGGING_CONFIG['loggers']['appbuilder']['level'] else: SIMPLE_HANDLERS_FILE["filename"] = filename + SIMPLE_HANDLERS_FILE_ERROR["filename"] = f"error.{filename}" LOGGING_CONFIG["handlers"]["file"] = SIMPLE_HANDLERS_FILE + LOGGING_CONFIG["handlers"]["error_file"] = SIMPLE_HANDLERS_FILE_ERROR + if "timed_file" in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("timed_file") + if "error_timed_file" in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("error_timed_file") + if "file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("file") + if "error_file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("error_file") LOGGING_CONFIG["handlers"]["file"]["level"] = LOGGING_CONFIG['loggers']['appbuilder']['level'] - logging.config.dictConfig(LOGGING_CONFIG) + logging.config.dictConfig(LOGGING_CONFIG) def setFilename(self, filename): """ set filename + + 设置此函数会同时设置日志文件和错误日志文件的文件名。同时取消了日志文件的滚动功能。 """ + if "timed_file" in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("timed_file") + if "error_timed_file" in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("error_timed_file") if "file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("file") + if "error_file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("error_file") SIMPLE_HANDLERS_FILE["filename"] = filename + SIMPLE_HANDLERS_FILE_ERROR["filename"] = f"error.{filename}" LOGGING_CONFIG["handlers"]["file"] = SIMPLE_HANDLERS_FILE + LOGGING_CONFIG["handlers"]["error_file"] = SIMPLE_HANDLERS_FILE_ERROR logging.config.dictConfig(LOGGING_CONFIG) def setLoglevel(self, level): @@ -194,6 +248,7 @@ def setLoglevel(self, level): LOGGING_CONFIG['handlers']['console']['level'] = log_level LOGGING_CONFIG['loggers']['appbuilder']['level'] = log_level SIMPLE_HANDLERS_FILE["level"] = log_level + TIME_HANDLERS_FILE['level'] = log_level logging.config.dictConfig(LOGGING_CONFIG) def process(self, msg, kwargs):