diff --git a/hm_pyhelper/tests/utils/test_logger.py b/hm_pyhelper/tests/utils/test_logger.py index a43d518..d2111ad 100644 --- a/hm_pyhelper/tests/utils/test_logger.py +++ b/hm_pyhelper/tests/utils/test_logger.py @@ -26,3 +26,21 @@ def test_logging(self): " - Hello world." are_logs_correct = re.search(expected_output_regex, formatted_output) self.assertTrue(are_logs_correct) + + def test_custom_log_format(self): + log_string = f"[%(levelname)s] - %(name)s: %(message)s" # noqa: F541 E501 + log_message = "test log string" + logger = get_logger(__name__, log_string) + + with self.assertLogs() as captured: + logger.info(log_message) + + # check that there is only one log message + self.assertEqual(len(captured.records), 1) + record = captured.records[0] + formatter = logging.Formatter(log_string) + formatted_output = formatter.format(record) + + result = re.match("\\[INFO\\]\\s-\\s.*:\\s" + log_message, + formatted_output) + self.assertTrue(result) diff --git a/hm_pyhelper/utils/logger.py b/hm_pyhelper/utils/logger.py index 2d4ee34..eb641a2 100644 --- a/hm_pyhelper/utils/logger.py +++ b/hm_pyhelper/utils/logger.py @@ -5,17 +5,17 @@ _log_format = f"%(asctime)s - [%(levelname)s] - %(name)s - (%(filename)s).%(funcName)s -- %(pathname)s:(%(lineno)d) - %(message)s" # noqa: F541 E501 -def get_stream_handler(): +def get_stream_handler(log_format=_log_format): stream_handler = logging.StreamHandler() stream_handler.setLevel(LOGLEVEL) - stream_handler.setFormatter(logging.Formatter(_log_format)) + stream_handler.setFormatter(logging.Formatter(log_format)) return stream_handler -def get_logger(name): +def get_logger(name, log_format=_log_format): logger = logging.getLogger(name) logger.setLevel(LOGLEVEL) - logger.addHandler(get_stream_handler()) + logger.addHandler(get_stream_handler(log_format)) return logger diff --git a/setup.py b/setup.py index c3194f3..ea66d37 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='hm_pyhelper', - version='0.8.2', + version='0.8.3', author="Nebra Ltd", author_email="support@nebra.com", description="Helium Python Helper",