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 Dec 20, 2024
1 parent 74c852e commit d4aa909
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 38 deletions.
44 changes: 10 additions & 34 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


@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,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'}})
Expand Down
63 changes: 63 additions & 0 deletions python/tests/test_utils_new_logging_util.py
Original file line number Diff line number Diff line change
@@ -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()

11 changes: 7 additions & 4 deletions python/utils/logger_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)


Expand Down

0 comments on commit d4aa909

Please sign in to comment.