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新增滚动日志 #678

Closed
wants to merge 7 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
38 changes: 37 additions & 1 deletion docs/BasisModule/Trace/Debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,40 @@ System.setProperty("APPBUILDER_LOGLFILE", "/tmp/appbuilder.log");
```golang
// golang
os.Setenv("APPBUILDER_LOGLEVEL", "/tmp/appbuilder.log")
```
```


## log高阶配置

`setLogConfig`功能可以设置日志的输出方式、是否滚动日志、滚动日志参数等。

参数说明:
- rolling (bool, optional): 是否启用滚动日志. 默认为True.
- update_interval (int, optional): 更新日志文件的间隔时间. 默认为1.
- update_time (str, optional): 更新日志文件的时间间隔单位. 默认为'midnight',每日凌晨更新.
- 可选值:
- 'S' - Seconds
- 'M' - Minutes
- 'H' - Hours
- 'D' - Days
- 'W0'-'W6' - Weekday (0=Monday, 6=Sunday)
- 'midnight' - Roll over at midnight
- backup_count (int, optional): 备份日志文件的数量. 默认为无穷大.
- filename (str, optional): 日志文件的名称. 默认为空字符串.

需在代码中设置

```python
appbuilder.logger.setLoglevel("DEBUG")
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

默认1,代表什么呢?
示例里面最好用1天,30s不太符合现实。

这里设置:
rolling=Ture - 启用滚动日志(运行此段代码,默认使用滚动日志)
filename="appbuilder.log" - 此优先级最高会覆盖之前的设置,若未传参则使用之前已经设置的日志文件,若之前未设置则使用默认的"tmp.log"日志文件
update_interval = 1 - 更新日志文件的间隔数量级
update_time = 'midnight' - 更新日志文件的间隔单位为秒级,每日凌晨滚动日志
"""
appbuilder.logger.setLogConfig(filename="appbuilder.log",update_interval=1, update_time='midnight')
```

## 新增功能
日志功能会自动开启日志的分离功能,独立创建一个`error.(filename).log'文件,用于存储`WARNING`、`ERROR`级别日志,同时会兼容日志的滚动功能。
26 changes: 20 additions & 6 deletions python/tests/test_utils_logging_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import json
import os
import copy

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", "")
class TestTestUtilsLoggingUtil(unittest.TestCase):
def setUp(self):
self.logger = LoggerWithLoggerId(LOGGING_CONFIG["loggers"]["appbuilder"], {}, 'DEBUG')
self.original_logging_config = copy.deepcopy(LOGGING_CONFIG)
print(json.dumps(LOGGING_CONFIG, indent=4, ensure_ascii=False))
self.logger = LoggerWithLoggerId(self.original_logging_config["loggers"]["appbuilder"], {}, 'DEBUG')

def tearDown(self):
global LOGGING_CONFIG
LOGGING_CONFIG = copy.deepcopy(self.original_logging_config)

def test_set_auto_logid(self):
self.logger.set_auto_logid()
Expand All @@ -31,13 +40,18 @@ 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_process(self):
msg,kwargs=self.logger.process(msg='test',kwargs={})
msg,kwargs=self.logger.process(msg='test',kwargs={'extra':{'logid':'test'}})


def test_set_log_config_rolling_false(self):
self.logger.setLogConfig(
rolling=False,
filename='test.log',
update_interval = -1,
update_time='M',
backup_count=-1
)


if __name__ == '__main__':
Expand Down
60 changes: 60 additions & 0 deletions python/tests/test_utils_new_logging_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (c) 2023 Baidu, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import json
import os
import copy

from appbuilder.utils.logger_util import LoggerWithLoggerId, LOGGING_CONFIG


@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_PARALLEL", "")
class TestTestUtilsLoggingUtil(unittest.TestCase):
def setUp(self):
self.original_logging_config = copy.deepcopy(LOGGING_CONFIG)
print(json.dumps(LOGGING_CONFIG, indent=4, ensure_ascii=False))
self.logger = LoggerWithLoggerId(self.original_logging_config["loggers"]["appbuilder"], {}, 'DEBUG')

def tearDown(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好有err日志文件

global LOGGING_CONFIG
LOGGING_CONFIG = copy.deepcopy(self.original_logging_config)


def test_set_log_config_01(self):
self.logger.setLogConfig(
update_interval = -1,
update_time='M',
backup_count=-1
)

def test_set_log_config_02(self):
self.logger.setLogConfig(
filename='test.log',
update_interval = -1,
update_time='M',
backup_count=-1
)

def test_set_log_config_03(self):
with self.assertRaises(ValueError):
self.logger.setLogConfig(
update_interval = -1,
update_time='Test',
backup_count=-1
)


if __name__ == '__main__':
unittest.main()

Loading
Loading