Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK新增滚动日志功能,为用户提供依据设置时间自动滚动日志 #674

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion python/tests/test_all_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -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入参测试类
Expand Down
36 changes: 33 additions & 3 deletions python/tests/test_utils_logging_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
Expand All @@ -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={})
Expand Down
68 changes: 68 additions & 0 deletions python/utils/logger_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import os
import sys
import logging.config
from logging.handlers import RotatingFileHandler, TimedRotatingFileHandler
from threading import current_thread


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading