Skip to content

Commit

Permalink
ORM: Cache the logger adapter for ProcessNode (aiidateam#6492)
Browse files Browse the repository at this point in the history
The logger adapter was recreated each time the `logger` property of the
`ProcessNode` was invoked. It is now created once in the `logger`
property. The created logger adapter is assigned to the `_logger_adapter`
attribute such that it can simply be returned at the next invocation.
The initialization of the adapter cannot be done in the constructor as
that route is not taken if an existing node is loaded from the database.

Finally, the `logger` property only creates and returns the adapter when
the node is stored. Otherwise it simply returns the base logger instance.
This is because the logger adapter only works for stored nodes and if it
were instantiated at the point when the node is unstored, it would not
be regenerated once the node is stored, and so the `DbLogHandler` will
never be able to persist log messages to the database.
  • Loading branch information
sphuber authored and mikibonacci committed Sep 3, 2024
1 parent 564f79b commit 353b34d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/aiida/orm/nodes/process/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,16 @@ def logger(self):
"""
from aiida.orm.utils.log import create_logger_adapter

return create_logger_adapter(self._logger, self)
# If the node is not yet stored, there is no point in creating the logger adapter yet, as the ``DbLogHandler``
# it configures, is only triggered for stored nodes, otherwise it cannot link the log message to the node.
if not self.pk:
return self._logger

# First time the property is called after the node is stored, create the logger adapter
if not hasattr(self, '_logger_adapter'):
self._logger_adapter = create_logger_adapter(self._logger, self)

return self._logger_adapter

@classmethod
def recursive_merge(cls, left: dict[Any, Any], right: dict[Any, Any]) -> None:
Expand Down

0 comments on commit 353b34d

Please sign in to comment.