diff --git a/log/log.py b/log/log.py index 8b6a94646..20a653bdd 100644 --- a/log/log.py +++ b/log/log.py @@ -300,6 +300,19 @@ def set_error_level(level): level = getattr(Log, level) Log.error.level = level + @staticmethod + def _verify_args_message(msg: str, *args): + """ + Verify if the message has arguments to format + + :param msg: message to show + :param args: arguments for message formatting + :return: message formatted + """ + if args: + msg = msg.format(*args) + return msg + @staticmethod def debug(msg, *args): """ @@ -308,7 +321,8 @@ def debug(msg, *args): :param msg: message to show :param args: arguments for message formating (it will be done using format() method on str) """ - Log.log.log(Log.DEBUG, msg.format(*args)) + msg = Log._verify_args_message(msg, *args) + Log.log.log(Log.DEBUG, msg) @staticmethod def info(msg, *args): @@ -318,7 +332,8 @@ def info(msg, *args): :param msg: message to show :param args: arguments for message formatting (it will be done using format() method on str) """ - Log.log.log(Log.INFO, msg.format(*args)) + msg = Log._verify_args_message(msg, *args) + Log.log.log(Log.INFO, msg) @staticmethod def result(msg, *args): @@ -328,7 +343,8 @@ def result(msg, *args): :param msg: message to show :param args: arguments for message formating (it will be done using format() method on str) """ - Log.log.log(Log.RESULT, msg.format(*args)) + msg = Log._verify_args_message(msg, *args) + Log.log.log(Log.RESULT, msg) @staticmethod def warning(msg, *args): @@ -338,7 +354,8 @@ def warning(msg, *args): :param msg: message to show :param args: arguments for message formatting (it will be done using format() method on str) """ - Log.log.log(Log.WARNING, msg.format(*args)) + msg = Log._verify_args_message(msg, *args) + Log.log.log(Log.WARNING, msg) @staticmethod def error(msg, *args): @@ -348,7 +365,8 @@ def error(msg, *args): :param msg: message to show :param args: arguments for message formatting (it will be done using format() method on str) """ - Log.log.log(Log.ERROR, msg.format(*args)) + msg = Log._verify_args_message(msg, *args) + Log.log.log(Log.ERROR, msg) @staticmethod def critical(msg, *args): @@ -358,7 +376,8 @@ def critical(msg, *args): :param msg: message to show :param args: arguments for message formatting (it will be done using format() method on str) """ - Log.log.log(Log.CRITICAL, msg.format(*args)) + msg = Log._verify_args_message(msg, *args) + Log.log.log(Log.CRITICAL, msg) @staticmethod def status(msg, *args): @@ -368,7 +387,8 @@ def status(msg, *args): :param msg: message to show :param args: arguments for message formatting (it will be done using format() method on str) """ - Log.log.log(Log.STATUS, msg.format(*args)) + msg = Log._verify_args_message(msg, *args) + Log.log.log(Log.STATUS, msg) @staticmethod def status_failed(msg, *args): @@ -378,7 +398,8 @@ def status_failed(msg, *args): :param msg: message to show :param args: arguments for message formatting (it will be done using format() method on str) """ - Log.log.log(Log.STATUS_FAILED, msg.format(*args)) + msg = Log._verify_args_message(msg, *args) + Log.log.log(Log.STATUS_FAILED, msg) @staticmethod def printlog(message="Generic message", code=4000): diff --git a/test/unit/test_log.py b/test/unit/test_log.py index e261b6429..14f7e75f6 100644 --- a/test/unit/test_log.py +++ b/test/unit/test_log.py @@ -1,6 +1,5 @@ from unittest import TestCase -from log.log import AutosubmitError, AutosubmitCritical - +from log.log import AutosubmitError, AutosubmitCritical, Log """Tests for the log module.""" @@ -28,3 +27,27 @@ def test_autosubmit_critical(self): assert None is ac.trace assert ' ' == str(ac) + def test_log_not_format(self): + """ + Smoke test if the log messages are sent correctly + when having a formattable message that it is not + intended to be formatted + """ + + def _send_messages(msg: str): + Log.debug(msg) + Log.info(msg) + Log.result(msg) + Log.warning(msg) + Log.error(msg) + Log.critical(msg) + Log.status(msg) + Log.status_failed(msg) + + # Standard messages + msg = "Test" + _send_messages(msg) + + # Format messages + msg = "Test {foo, bar}" + _send_messages(msg)