Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yinjiaqi authored and yinjiaqi committed Jan 2, 2025
1 parent 849d7ef commit 2d4c24d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 52 deletions.
26 changes: 14 additions & 12 deletions docs/BasisModule/Trace/Debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,31 @@ os.Setenv("APPBUILDER_LOGLEVEL", "/tmp/appbuilder.log")
Appbuilder-SDK新增滚动日志功能

主要参数:
- console_show: 数据类型bool,默认值True,LOG日志是否在控制台输出
- console_output: 数据类型bool,默认值True,LOG日志是否在控制台输出
- loglevel: 数据类型str,默认值"DEBUG",LOG日志级别
- file_name: 数据类型str,默认值"tmp.log",LOG日志名称
- when: 数据类型str,默认值"MIDNIGHT",LOG日志滚动更新时间单位
- rotate_frequency: 数据类型str,默认值"MIDNIGHT",LOG日志滚动更新时间单位
- "S": 以秒为单位
- "M": 以分钟为单位
- "H": 以小时为单位
- "D": 以天为时间单位
- "MIDNIGHT": 每日凌晨更新
- interval: 数据类型int,默认值1,LOG日志按时间滚动的参数,默认值为1,与when参数联合使用
- max_bytes: 数据类型Optional[int],默认值None,传入`None`或负数会自动更新为系统最大整数`sys.maxsize`,单个滚动的LOG日志文件的最大大小,例:10M即为10\*1024\*1024 即需要传入 # 以B为单位
- total_size_limit: 数据类型Optional[int],默认值None,传入`None`或负数会自动更新为系统最大整数`sys.maxsize`,当前目录下可储存的LOG日志文件的最大大小,例:10M即为10\*1024\*1024 # 以B为单位
- backup_count: 数据类型Optional[int],默认值None,传入`None`或负数会自动更新为系统最大整数`sys.maxsize`,当前目录下可储存的LOG日志文件的最大数量
- rotate_interval: 数据类型int,默认值1,LOG日志按时间滚动的参数,默认值为1,与when参数联合使用
- max_file_size: 数据类型Optional[int],默认值None,传入`None`或负数会自动更新为系统最大整数`sys.maxsize`,单个滚动的LOG日志文件的最大大小,例:10M即为10\*1024\*1024 即需要传入 # 以B为单位
- total_log_size: 数据类型Optional[int],默认值None,传入`None`或负数会自动更新为系统最大整数`sys.maxsize`,当前目录下可储存的LOG日志文件的最大大小,例:10M即为10\*1024\*1024 # 以B为单位
- max_log_files: 数据类型Optional[int],默认值None,传入`None`或负数会自动更新为系统最大整数`sys.maxsize`,当前目录下可储存的LOG日志文件的最大数量

**注意:`setLogConfig`会自动生成error.file_name日志与file_name日志文件分别储存`error`级别日志和`loglevel`级别的日志,且两种日志文件的滚动逻辑是独立的,不相互影响。**
```python
# python
appbuilder.logger.setLogConfig(
console_show = False,
console_output = False,
loglevel="DEBUG"
file_name="appbuilder.log",
when="MIDNIGHT", # 每日凌晨更新
interval=1,
max_bytes=100 * 1024 *1024, # 最大日志大小为100MB
total_size_limit=1024 * 1024 *1024, # 最大储存1GB的日志
backup_count=10, # 当前目录储存的最大LOG日志数
rotate_frequency="MIDNIGHT", # 每日凌晨更新
rotate_interval=1,
max_file_size=100 * 1024 *1024, # 最大日志大小为100MB
total_log_size=1024 * 1024 *1024, # 最大储存1GB的日志
max_log_files=10, # 当前目录储存的最大LOG日志数
)
```
37 changes: 25 additions & 12 deletions python/tests/test_log_set_log_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,42 @@ class TestLogSetLogConfig(unittest.TestCase):
def test_set_log_config(self):
lwl=LoggerWithLoggerId(logger='test_logger',extra={'logid':'test_logid'},loglevel='INFO')
lwl.setLogConfig(
console_show = True,
console_output = True,
loglevel='DEBUG',
file_name='test.log',
when='D',
interval=0, # 测试interval<1时,自动更新为1
max_bytes=None, # 测试not max_bytes or max_bytes <= 0时,自动更新为sys.maxsize
total_size_limit=None, # 测试not total_size_limit or total_size_limit <= 0时,自动更新为sys.maxsize
backup_count=None, # 测试not backup_count or backup_count <= 0时,自动更新为sys.maxsize
rotate_frequency='D',
rotate_interval=0, # 测试rotate_interval<1时,自动更新为1
max_file_size=None, # 测试not max_file_size or max_file_size <= 0时,自动更新为sys.maxsize
total_log_size=None, # 测试not total_log_size or total_log_size <= 0时,自动更新为sys.maxsize
max_log_files=None, # 测试not max_log_files or max_log_files <= 0时,自动更新为sys.maxsize
)

def test_set_log_config_raise_error(self):
lwl=LoggerWithLoggerId(logger='test_logger',extra={'logid':'test_logid'},loglevel='INFO')
with self.assertRaises(ValueError):
lwl.setLogConfig(
console_show = True,
console_output = True,
loglevel='DEBUG',
file_name='test.log',
when='ERROR-WHEN',
interval=0, # 测试interval<1时,自动更新为1
max_bytes=None, # 测试not max_bytes or max_bytes <= 0时,自动更新为sys.maxsize
total_size_limit=None, # 测试not total_size_limit or total_size_limit <= 0时,自动更新为sys.maxsize
backup_count=None, # 测试not backup_count or backup_count <= 0时,自动更新为sys.maxsize
rotate_frequency='ERROR-FREQUENCY',
rotate_interval=0, # 测试rotate_interval<1时,自动更新为1
max_file_size=None, # 测试not max_file_size or max_file_size <= 0时,自动更新为sys.maxsize
total_log_size=None, # 测试not total_log_size or total_log_size <= 0时,自动更新为sys.maxsize
max_log_files=None, # 测试not max_log_files or max_log_files <= 0时,自动更新为sys.maxsize
)

with self.assertRaises(ValueError):
lwl.setLogConfig(
console_output = True,
loglevel='ERROR-LEVEL',
file_name='test.log',
rotate_frequency='D',
rotate_interval=0, # 测试rotate_interval<1时,自动更新为1
max_file_size=0, # 测试not max_file_size or max_file_size <= 0时,自动更新为sys.maxsize
total_log_size=None, # 测试not total_log_size or total_log_size <= 0时,自动更新为sys.maxsize
max_log_files=None, # 测试not max_log_files or max_log_files <= 0时,自动更新为sys.maxsize
)


def test_rolling_with_time(self):
time_msgs = ['S', 'M', 'H', 'D', 'MIDNIGHT']
Expand Down
62 changes: 34 additions & 28 deletions python/utils/logger_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,37 +180,43 @@ def setLoglevel(self, level):
logging.config.dictConfig(LOGGING_CONFIG)

def setLogConfig(self,
console_show: bool = True,
console_output: bool = True,
loglevel: str = "DEBUG",
file_name: str = "tmp.log",
when: str = "MIDNIGHT",
interval: int = 1,
max_bytes: Optional[int] = None, # 以B为单位
total_size_limit: Optional[int] = None, # 以B为单位
backup_count: Optional[int] = None
rotate_frequency: str = "MIDNIGHT",
rotate_interval: int = 1,
max_file_size: Optional[int] = None, # 以B为单位
total_log_size: Optional[int] = None, # 以B为单位
max_log_files: Optional[int] = None
):
LOGGING_CONFIG["handlers"] = {}
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"] = []

# log_level 数据校验
log_level = loglevel.strip().lower()
if log_level not in ["debug", "info", "warning", "error"]:
raise ValueError("expected APPBUILDER_LOGLEVEL in [debug, info, warning, error], but got %s" % log_level)

# 设置console输出日志
if console_show:
if console_output:
CONSOLE_HEADER['level'] = loglevel
LOGGING_CONFIG["handlers"]["console"] = CONSOLE_HEADER
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("console")
else:
LOGGING_CONFIG["loggers"]["appbuilder"]["propagate"] = False

# 参数验证
if not max_bytes or max_bytes <= 0:
max_bytes = sys.maxsize
if not total_size_limit or total_size_limit <= 0:
total_size_limit = sys.maxsize
if not backup_count or backup_count <= 0:
backup_count = sys.maxsize
if interval < 1:
interval = 1
when = when.strip().lower()
if when not in ["s", "m", "h", "d", "midnight"]:
raise ValueError("expected when in [S, M, H, D, MIDNIGHT], but got %s" % when)
if not max_file_size or max_file_size <= 0:
max_file_size = sys.maxsize
if not total_log_size or total_log_size <= 0:
total_log_size = sys.maxsize
if not max_log_files or max_log_files <= 0:
max_log_files = sys.maxsize
if rotate_interval < 1:
rotate_interval = 1
rotate_frequency = rotate_frequency.strip().lower()
if rotate_frequency not in ["s", "m", "h", "d", "midnight"]:
raise ValueError("expected rotate_frequency in [S, M, H, D, MIDNIGHT], but got %s" % rotate_frequency)

# 设置文件输出日志
# 设置日志级别
Expand All @@ -221,23 +227,23 @@ def setLogConfig(self,
ERROR_SET_CONFIG_HEADER['filename'] = _add_error_to_file_name(file_name)

# 设置滚动时间
SET_CONFIG_HEADER['when'] = when
ERROR_SET_CONFIG_HEADER['when'] = when
SET_CONFIG_HEADER['interval'] = interval
ERROR_SET_CONFIG_HEADER['interval'] = interval
SET_CONFIG_HEADER['when'] = rotate_frequency
ERROR_SET_CONFIG_HEADER['when'] = rotate_frequency
SET_CONFIG_HEADER['interval'] = rotate_interval
ERROR_SET_CONFIG_HEADER['interval'] = rotate_interval

# 设置最大文件大小

SET_CONFIG_HEADER['max_bytes'] = max_bytes
ERROR_SET_CONFIG_HEADER['max_bytes'] = max_bytes
SET_CONFIG_HEADER['max_bytes'] = max_file_size
ERROR_SET_CONFIG_HEADER['max_bytes'] = max_file_size

# 设置总大小限制
SET_CONFIG_HEADER['total_size_limit'] = total_size_limit
ERROR_SET_CONFIG_HEADER['total_size_limit'] = total_size_limit
SET_CONFIG_HEADER['total_size_limit'] = total_log_size
ERROR_SET_CONFIG_HEADER['total_size_limit'] = total_log_size

# 设置备份数量
SET_CONFIG_HEADER['backup_count'] = backup_count
ERROR_SET_CONFIG_HEADER['backup_count'] = backup_count
SET_CONFIG_HEADER['backup_count'] = max_log_files
ERROR_SET_CONFIG_HEADER['backup_count'] = max_log_files

LOGGING_CONFIG["handlers"]["file"] = SET_CONFIG_HEADER
LOGGING_CONFIG["handlers"]["error_file"] = ERROR_SET_CONFIG_HEADER
Expand Down

0 comments on commit 2d4c24d

Please sign in to comment.