diff --git a/aiida/cmdline/utils/log.py b/aiida/cmdline/utils/log.py index dbc715e7ad..69b7c7d11e 100644 --- a/aiida/cmdline/utils/log.py +++ b/aiida/cmdline/utils/log.py @@ -51,7 +51,15 @@ def format(record): except KeyError: fg = 'white' - if record.prefix: + try: + prefix = record.prefix + except AttributeError: + prefix = None + + if prefix: return f'{click.style(record.levelname.capitalize(), fg=fg, bold=True)}: {record.msg % record.args}' - return f'{record.msg % record.args}' + if record.args: + return f'{record.msg % record.args}' + + return record.msg diff --git a/tests/cmdline/utils/test_log.py b/tests/cmdline/utils/test_log.py new file mode 100644 index 0000000000..a5166f75e0 --- /dev/null +++ b/tests/cmdline/utils/test_log.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +########################################################################### +# Copyright (c), The AiiDA team. All rights reserved. # +# This file is part of the AiiDA code. # +# # +# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core # +# For further information on the license, see the LICENSE.txt file # +# For further information please visit http://www.aiida.net # +########################################################################### +"""Tests for the :mod:`aiida.cmdline.utils.log` module.""" +import logging + +from aiida.cmdline.utils import log + + +def test_cli_formatter(): + """Test the ``CliFormatter.format`` method for a plain message. + + Note that if it contains percentage signs but no arguments, it should not try to convert it. + """ + message = 'message' + record = logging.LogRecord('name', logging.INFO, 'pathname', 0, message, (), 'exc_info') + assert log.CliFormatter.format(record) == message + + +def test_cli_formatter_no_args(): + """Test the ``CliFormatter.format`` method for a message with percentage signs but no args.""" + message = 'PID MEM % CPU % started' + record = logging.LogRecord('name', logging.INFO, 'pathname', 0, message, (), 'exc_info') + assert log.CliFormatter.format(record) == message + + +def test_cli_formatter_args(): + """Test the ``CliFormatter.format`` method for a message with a single argument.""" + record = logging.LogRecord('name', logging.INFO, 'pathname', 0, 'Some %s', ('value',), 'exc_info') + assert log.CliFormatter.format(record) == 'Some value' + + +def test_cli_formatter_prefix(): + """Test the ``CliFormatter.format`` method for a message with a single argument.""" + record = logging.LogRecord('name', logging.INFO, 'pathname', 0, 'Some %s', ('value',), 'exc_info') + record.prefix = True + assert log.CliFormatter.format(record) == '\x1b[34m\x1b[1mInfo\x1b[0m: Some value'