-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: ensure all loggers have a custom formatter
So far, the utility functions of the `aiida.cmdline.utils.echo` module have been rerouted through the `CMDLINE_LOGGER` which properly formats the log message with just the message itself. However, the loggers of any other modules will inherit the formatter of the parent `AIIDA_LOGGER` which will prefix the message with verbose content such as the timestamp, name of the logger and more, which is not what we want for output generated by CLI commands. The solution is to define a custom formatter, `CliFormatter`, which is added to the new module `aiida.cmdline.utils.log`, and which is configured by default for the `CMDLINE_LOGGER`. The `configure_logging` function is updated to now take a keyword argument `cli`, which when set to `True`, will configure this new formatter for all loggers. By calling this method with `cli=True` in the callback of the `VERBOSITY` option, all loggers should be correctly configured to only print the message with the log level prefixed, when invoked through the CLI. There is one peculiarity where the `load_backend_if_not_loaded` decorator utility also needs to call the `configure_logging` function. The reason is that this function loads the database backend, which indirectly calls the `configure_logging` to add the database log handler, but this does not pass `cli=True`. This means that the CLI specific configuration is undone.
- Loading branch information
Showing
5 changed files
with
91 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Utilities for logging in the command line interface context.""" | ||
import logging | ||
|
||
import click | ||
|
||
from .echo import COLORS | ||
|
||
|
||
class CliHandler(logging.Handler): | ||
"""Handler for writing to the console using click.""" | ||
|
||
def emit(self, record): | ||
"""Emit log record via click. | ||
Can make use of special attributes 'nl' (whether to add newline) and 'err' (whether to print to stderr), which | ||
can be set via the 'extra' dictionary parameter of the logging methods. | ||
""" | ||
try: | ||
nl = record.nl | ||
except AttributeError: | ||
nl = True | ||
|
||
try: | ||
err = record.err | ||
except AttributeError: | ||
err = False | ||
|
||
try: | ||
prefix = record.prefix | ||
except AttributeError: | ||
prefix = True | ||
|
||
record.prefix = prefix | ||
|
||
try: | ||
msg = self.format(record) | ||
click.echo(msg, err=err, nl=nl) | ||
except Exception: # pylint: disable=broad-except | ||
self.handleError(record) | ||
|
||
|
||
class CliFormatter(logging.Formatter): | ||
"""Formatter that automatically prefixes log messages with a colored version of the log level.""" | ||
|
||
@staticmethod | ||
def format(record): | ||
"""Format the record using the style required for the command line interface.""" | ||
try: | ||
fg = COLORS[record.levelname.lower()] | ||
except KeyError: | ||
fg = 'white' | ||
|
||
if record.prefix: | ||
return f'{click.style(record.levelname.capitalize(), fg=fg, bold=True)}: {record.msg % record.args}' | ||
|
||
return f'{record.msg % record.args}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters