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

CLI: Fix error in aiida.cmdline.utils.log.CliFormatter #5957

Merged
merged 3 commits into from
Apr 11, 2023
Merged
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
12 changes: 5 additions & 7 deletions aiida/cmdline/utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def emit(self, record):
class CliFormatter(logging.Formatter):
"""Formatter that automatically prefixes log messages with a colored version of the log level."""

@staticmethod
def format(record):
def format(self, record):
"""Format the record using the style required for the command line interface."""
try:
fg = COLORS[record.levelname.lower()]
Expand All @@ -56,10 +55,9 @@ def format(record):
except AttributeError:
prefix = None

if prefix:
return f'{click.style(record.levelname.capitalize(), fg=fg, bold=True)}: {record.msg % record.args}'
formatted = super().format(record)

if record.args:
return f'{record.msg % record.args}'
if prefix:
return f'{click.style(record.levelname.capitalize(), fg=fg, bold=True)}: {formatted}'

return record.msg
return formatted
28 changes: 20 additions & 8 deletions tests/cmdline/utils/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,37 @@ def test_cli_formatter():
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
record = logging.LogRecord('name', logging.INFO, 'pathname', 0, message, (), None)
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
record = logging.LogRecord('name', logging.INFO, 'pathname', 0, message, (), None)
assert log.CliFormatter().format(record) == message


def test_cli_formatter_named_parameters():
"""Test the ``CliFormatter.format`` method for a message with named parameters but no args.

This can occur for example when logging prepared SQL queries. These contain named parameters, but are not actually
intended to be filled with arguments passed to the logging call but are supposed to be kept as is. When no arguments
are passed in the log record, this should not except.
"""
message = 'SELECT t.pk FROM t WHERE t.pk = %(pk_1)s'
record = logging.LogRecord('name', logging.INFO, 'pathname', 0, message, (), None)
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'
record = logging.LogRecord('name', logging.INFO, 'pathname', 0, 'Some %s', ('value',), None)
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 = logging.LogRecord('name', logging.INFO, 'pathname', 0, 'Some %s', ('value',), None)
record.prefix = True
assert log.CliFormatter.format(record) == '\x1b[34m\x1b[1mInfo\x1b[0m: Some value'
assert log.CliFormatter().format(record) == '\x1b[34m\x1b[1mInfo\x1b[0m: Some value'