diff --git a/python/tests/test_utils_logging_util.py b/python/tests/test_utils_logging_util.py index 283128d4..1ca7e81c 100644 --- a/python/tests/test_utils_logging_util.py +++ b/python/tests/test_utils_logging_util.py @@ -12,7 +12,10 @@ # 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 @@ -20,7 +23,13 @@ @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() @@ -31,39 +40,6 @@ def test_set_logid(self): def test_get_logid(self): self.logger.set_auto_logid() - def test_set_log_config(self): - self.logger.setLogConfig( - console_show=False, - update_interval = -1, - update_time='M', - backup_count=-1 - ) - - self.logger.setLogConfig( - filename='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={}) msg,kwargs=self.logger.process(msg='test',kwargs={'extra':{'logid':'test'}}) diff --git a/python/tests/test_utils_new_logging_util.py b/python/tests/test_utils_new_logging_util.py new file mode 100644 index 00000000..221278ff --- /dev/null +++ b/python/tests/test_utils_new_logging_util.py @@ -0,0 +1,63 @@ +# 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): + global LOGGING_CONFIG + LOGGING_CONFIG = copy.deepcopy(self.original_logging_config) + + + def test_set_log_config_01(self): + self.logger.setLogConfig( + console_show=False, + update_interval = -1, + update_time='M', + backup_count=-1 + ) + + def test_set_log_config_02(self): + self.logger.setLogConfig( + filename='test.log', + console_show=False, + update_interval = -1, + update_time='M', + backup_count=-1 + ) + + def test_set_log_config_03(self): + with self.assertRaises(ValueError): + self.logger.setLogConfig( + console_show=False, + update_interval = -1, + update_time='Test', + backup_count=-1 + ) + + +if __name__ == '__main__': + unittest.main() + \ No newline at end of file diff --git a/python/utils/logger_util.py b/python/utils/logger_util.py index 0872aad8..39a14e53 100644 --- a/python/utils/logger_util.py +++ b/python/utils/logger_util.py @@ -80,6 +80,7 @@ def __init__(self, logger, extra, loglevel): """ log_file = os.environ.get("APPBUILDER_LOGFILE", "") if log_file: + LOGGING_CONFIG["loggers"]["appbuilder"]["handler"] = ["console"] # 默认使用console SIMPLE_HANDLERS_FILE["filename"] = log_file TIME_HANDLERS_FILE['filename'] = log_file SIMPLE_HANDLERS_FILE["level"] = loglevel @@ -152,7 +153,7 @@ def setLogConfig( # 设置filename if not filename: - if SIMPLE_HANDLERS_FILE["filename"] or TIME_HANDLERS_FILE["backupCount"]: + if SIMPLE_HANDLERS_FILE["filename"] or TIME_HANDLERS_FILE["filename"]: if not SIMPLE_HANDLERS_FILE["filename"]: filename = SIMPLE_HANDLERS_FILE["filename"] else: @@ -167,14 +168,16 @@ def setLogConfig( TIME_HANDLERS_FILE['interval'] = update_interval TIME_HANDLERS_FILE['backupCount'] = backup_count TIME_HANDLERS_FILE['filename'] = filename - LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("file") - LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append('timed_file') + if 'file' in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]: + LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("file") + if not "timed_file" in LOGGING_CONFIG["handlers"]: + 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: SIMPLE_HANDLERS_FILE["filename"] = filename LOGGING_CONFIG["handlers"]["file"] = SIMPLE_HANDLERS_FILE - print(json.dumps(LOGGING_CONFIG, indent=4 ,ensure_ascii=False)) + LOGGING_CONFIG["handlers"]["file"]["level"] = LOGGING_CONFIG['loggers']['appbuilder']['level'] logging.config.dictConfig(LOGGING_CONFIG)