-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize Towhee logging module (#2640)
Signed-off-by: Kaiyuan Hu <kaiyuan.hu@zilliz.com>
- Loading branch information
Showing
4 changed files
with
254 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Copyright 2021 Zilliz. 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 re | ||
import logging | ||
import unittest | ||
from pathlib import Path | ||
from io import StringIO | ||
|
||
from towhee.config import LogConfig | ||
from towhee.utils.log import engine_log, enable_log_config | ||
|
||
|
||
class TestLog(unittest.TestCase): | ||
""" | ||
Test towhee logs. | ||
""" | ||
def test_log(self): | ||
f = StringIO() | ||
|
||
with self.assertRaises(ValueError): | ||
config = LogConfig(mode='test') | ||
|
||
with self.assertRaises(ValueError): | ||
config = LogConfig(rotate_mode='test') | ||
|
||
config = LogConfig(mode='console', rotate_mode='time', stream=f) | ||
root = logging.getLogger() | ||
|
||
self.assertEqual(root.level, 30) | ||
self.assertEqual(engine_log.level, 0) | ||
|
||
# Default stream log | ||
enable_log_config() | ||
engine_log.warning('test_warning') | ||
pattern = r'\d*-\d*-\d* \d*:\d*:\d*,\d* - \d* - test_log.py-test_log:\d* - WARNING: test_warning' | ||
self.assertTrue(re.search(pattern, f.getvalue()) is not None) | ||
self.assertEqual(root.level, 30) | ||
|
||
# Debug level stream log | ||
config.stream_level = 'DEBUG' | ||
enable_log_config(config) | ||
engine_log.debug('test_debug') | ||
pattern = r'\d*-\d*-\d* \d*:\d*:\d*,\d* - \d* - test_log.py-test_log:\d* - DEBUG: test_debug' | ||
self.assertTrue(re.search(pattern, f.getvalue()) is not None) | ||
self.assertEqual(root.level, 10) | ||
|
||
f.close() | ||
|
||
# Default file log | ||
config.mode = 'file' | ||
enable_log_config(config) | ||
engine_log.info('test_info') | ||
pattern = r'\d*-\d*-\d* \d*:\d*:\d*,\d* - \d* - test_log.py-test_log:\d* - INFO: test_info' | ||
with open('towhee.log', encoding='utf-8') as file: | ||
self.assertTrue(re.search(pattern, file.read()) is not None) | ||
self.assertEqual(root.level, 20) | ||
self.assertIsInstance(root.handlers[-1], logging.handlers.TimedRotatingFileHandler) | ||
Path('towhee.log').unlink() | ||
|
||
# Error level file log | ||
config.mode = 'file' | ||
config.rotate_mode = 'size' | ||
config.file_level = 'ERROR' | ||
enable_log_config(config) | ||
engine_log.error('test_error') | ||
pattern = r'\d*-\d*-\d* \d*:\d*:\d*,\d* - \d* - test_log.py-test_log:\d* - ERROR: test_error' | ||
with open('towhee.log', encoding='utf-8') as file: | ||
self.assertTrue(re.search(pattern, file.read()) is not None) | ||
self.assertEqual(root.level, 40) | ||
self.assertIsInstance(root.handlers[-1], logging.handlers.RotatingFileHandler) | ||
Path('towhee.log').unlink() | ||
|
||
with self.assertRaises(ValueError): | ||
config.mode = 'test' | ||
enable_log_config() | ||
|
||
with self.assertRaises(ValueError): | ||
config.rotate_mode = 'test' | ||
enable_log_config() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright 2021 Zilliz. 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. | ||
|
||
from .log_config import LogConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright 2021 Zilliz. 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. | ||
from typing import Optional, Any | ||
|
||
from pydantic import BaseModel, validator | ||
from towhee.utils.singleton import singleton | ||
|
||
|
||
@singleton | ||
class LogConfig(BaseModel): | ||
""" | ||
Towhee logger config. | ||
Args: | ||
mode (`Optional[str]`): | ||
The mode of Logger, decide which Handler to use, 'file' or 'console', defaults to 'console'. | ||
rotate_mode (`optional[str]`): | ||
The mode of rotating files, 'time' or 'size', defaults to 'time'. | ||
filename (`Optional[str]`): | ||
Path for log files, defaults to 'towhee.log'. | ||
rotate_when (`Optional[str]`): | ||
The type of TimedRotatingFileHandler interval, supports 'S', 'M', 'H', 'D', 'W0'-'W6', 'midnight', deafults to 'D'. | ||
rotate_interval (`Optional[int]`): | ||
The interval value of timed rotating, defaults to 1. | ||
backup_count (`Optional[int]`): | ||
The number of log files to keep, defaults to 10. | ||
stream_level (`Optional[str]`): | ||
The log level in console mode, defaults to 'WARNING'. | ||
file_level (`Optional[str]`): | ||
The log level in file mode, defautls to 'INFO'. | ||
utc (`Optional[bool]`): | ||
Whether to use utc time, defaults to False. | ||
stream (`Optional[Any]`): | ||
The stream to write logs into in console mode, defeaults to None and sys.stderr is used. | ||
""" | ||
mode: Optional[str] = 'console' | ||
rotate_mode: Optional[str] = 'time' | ||
filename: Optional[str] = 'towhee.log' | ||
file_max_size: Optional[int] = 100 * 1000 * 1000 | ||
rotate_when: Optional[str] = 'D' | ||
rotate_interval: Optional[int] = 1 | ||
backup_count: Optional[int] = 10 | ||
stream_level: Optional[str] = 'WARNING' | ||
file_level: Optional[str] = 'INFO' | ||
utc: Optional[bool] = False | ||
stream: Optional[Any] = None | ||
|
||
@validator('mode') | ||
def mode_range(cls, mode): # pylint: disable=no-self-argument | ||
if mode not in ['console', 'file']: | ||
raise ValueError(f'value should be either \'console\' or \'file\', not \'{mode}\'.') | ||
return mode | ||
|
||
@validator('rotate_mode') | ||
def rotate_mode_range(cls, rotate_mode): # pylint: disable=no-self-argument | ||
if rotate_mode not in ['time', 'size']: | ||
raise ValueError(f'value should be either \'time\' or \'size\', not \'{rotate_mode}\'.') | ||
return rotate_mode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters