diff --git a/install.sh b/install.sh new file mode 100644 index 00000000..b7ce022b --- /dev/null +++ b/install.sh @@ -0,0 +1,5 @@ +# linux系统快捷安装包shell脚本 +python3 -m pip uninstall appbuilder-sdk -y +rm -rf dist +python3 -u setup.py bdist_wheel +python3 -m pip install dist/*.whl diff --git a/python/tests/test_all_components.py b/python/tests/test_all_components.py index 83c605e1..20c13d95 100644 --- a/python/tests/test_all_components.py +++ b/python/tests/test_all_components.py @@ -85,7 +85,6 @@ def write_error_data(txt_file_path, error_df, error_stats): file.write(f"错误信息: {error}, 出现次数: {count}\n") print(f"\n错误信息已写入: {txt_file_path}") -@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_PARALLEL", "") class TestComponentManifestsAndToolEval(unittest.TestCase): """ 组件manifests和tool_eval入参测试类 diff --git a/python/tests/test_utils_logging_util.py b/python/tests/test_utils_logging_util.py index 0022e25d..df71c079 100644 --- a/python/tests/test_utils_logging_util.py +++ b/python/tests/test_utils_logging_util.py @@ -14,7 +14,7 @@ import unittest import os -from appbuilder.utils.logger_util import LoggerWithLoggerId,LOGGING_CONFIG +from appbuilder.utils.logger_util import LoggerWithLoggerId, LOGGING_CONFIG @unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_PARALLEL", "") @@ -31,8 +31,38 @@ def test_set_logid(self): def test_get_logid(self): self.logger.set_auto_logid() - # def test_level(self): - # level=self.logger.level() + def test_set_log_config(self): + self.logger.setLogConfig( + console_show=False, + update_interval = -1, + update_time='M', + backup_count=-1 + ) + + self.logger.setLogConfig( + log_file='test.log', + console_show=False, + update_interval = -1, + update_time='M', + backup_count=-1 + ) + + os.environ["APPBUILDER_LOGFILE"] = 'test.log' + self.logger.setLogConfig( + console_show=False, + update_interval = -1, + update_time='M', + backup_count=-1 + ) + del os.environ['APPBUILDER_LOGFILE'] + + with self.assertRaises(ValueError): + self.logger.setLogConfig( + console_show=False, + update_interval = -1, + update_time='Test', + backup_count=-1 + ) def test_process(self): msg,kwargs=self.logger.process(msg='test',kwargs={}) diff --git a/python/utils/logger_util.py b/python/utils/logger_util.py index 3396567d..16a38416 100644 --- a/python/utils/logger_util.py +++ b/python/utils/logger_util.py @@ -20,6 +20,7 @@ import os import sys import logging.config +from logging.handlers import RotatingFileHandler, TimedRotatingFileHandler from threading import current_thread @@ -55,6 +56,18 @@ } +TIME_HANDLERS_FILE = { + 'class': 'logging.handlers.TimedRotatingFileHandler', + 'formatter': 'standard', + 'level': 'DEBUG', + 'filename': 'tmp.log', + 'when': 'midnight', # 可选项: 'S', 'M', 'H', 'D', 'W0'-'W6', 'midnight' + 'interval': 1, # 每1天滚动一次 + 'backupCount': 5, # 保留5个备份 + 'encoding': 'utf-8', +} + + class LoggerWithLoggerId(logging.LoggerAdapter): """ logger with logid @@ -102,6 +115,61 @@ def level(self): """ return self.logger.level + def setLogConfig( + self, + rolling=True, + console_show=True, + update_interval:int=1, + update_time='', + backup_count=0, + log_file='' + ): + # 配置控制台输出 + if not console_show: + if "console" in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("console") + if "file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("file") + + # 确定日志文件名称 + if log_file: + filename = log_file + elif os.environ.get("APPBUILDER_LOGFILE"): + filename = os.environ["APPBUILDER_LOGFILE"] + else: + filename = LOGGING_CONFIG["handlers"]["file"]["filename"] + + # 确定备份数量 + if backup_count <= 0 or not isinstance(backup_count, int): + backup_count = sys.maxsize # 默认为无穷大 + + # 确定滚动时间 + if update_interval < 1: + update_interval = 1 + if update_time: + update_time = update_time.lower() + if update_time not in ['s', 'm', 'h', 'd', 'midnight'] and not (update_time.startswith('w') and update_time[1:].isdigit() and 0 <= int(update_time[1:]) <= 6): + raise ValueError("expected APPBUILDER_LOG_UPDATE_TIME in [s, m, h, d, w0-w6, midnight], but got %s" % update_time) + else: + update_time = update_time.upper() + + # 创建处理器 + if rolling: + if update_time: + TIME_HANDLERS_FILE['filename'] = filename + TIME_HANDLERS_FILE['when'] = update_time + TIME_HANDLERS_FILE['interval'] = update_interval + TIME_HANDLERS_FILE['backupCount'] = backup_count + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("file") + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append('timed_file') + LOGGING_CONFIG["handlers"]["timed_file"] = TIME_HANDLERS_FILE + LOGGING_CONFIG["handlers"]["timed_file"]["level"] = LOGGING_CONFIG['loggers']['appbuilder']['level'] + else: + LOGGING_CONFIG["handlers"]["file"]["filename"] = filename + + logging.config.dictConfig(LOGGING_CONFIG) + + def setFilename(self, filename): """ set filename