Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify args to format message #2113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions log/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down
27 changes: 25 additions & 2 deletions test/unit/test_log.py
Original file line number Diff line number Diff line change
@@ -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."""

Expand Down Expand Up @@ -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)