Skip to content

Commit

Permalink
Add the report method to logging.LoggerAdapter (#6186)
Browse files Browse the repository at this point in the history
AiiDA defines the `REPORT` log level and adds the `report` method to the
`logging.Logger` class so a log message can easily be emitted at that
level. However, the logger of `Process` instances is a `LoggerAdapter`
which does not inherit from `Logger` so the method also needs to be
added there independently. Without this fix, calling `self.logger.report` in
the `Parser.parse` method would raise an `AttributeError`.
  • Loading branch information
sphuber committed Nov 21, 2023
1 parent c2fcad4 commit 7d6684c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions aiida/common/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def report(self, msg: str, *args, **kwargs) -> None:

setattr(logging, 'REPORT', LOG_LEVEL_REPORT)
setattr(logging.Logger, 'report', report)
setattr(logging.LoggerAdapter, 'report', report)

# Convenience dictionary of available log level names and their log level integer
LOG_LEVELS = {
Expand Down
11 changes: 10 additions & 1 deletion tests/parsers/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ def parse(self, **kwargs):
self.out('invalid_output', orm.Str('0'))


class ReportArithmeticAddParser(Parser):
"""Parser for an ``ArithmeticAddCalculation`` job that just logs a message at the ``REPORT`` level."""

def parse(self, **kwargs):
"""Log a message at the ``REPORT`` level."""
self.logger.report('test the report method.')
self.out('sum', orm.Int(3))


class TestParser:
"""Test backend entities and their collections"""

Expand Down Expand Up @@ -125,7 +134,7 @@ def test_parse_from_node(self):
retrieved.store()
retrieved.base.links.add_incoming(node, link_type=LinkType.CREATE, link_label='retrieved')

for cls in [ArithmeticAddParser, SimpleArithmeticAddParser]:
for cls in [ArithmeticAddParser, SimpleArithmeticAddParser, ReportArithmeticAddParser]:
result, calcfunction = cls.parse_from_node(node)

assert isinstance(result['sum'], orm.Int)
Expand Down

0 comments on commit 7d6684c

Please sign in to comment.