Logging¶
- poriscope.utils.LogDecorator.log(_func=None, *, logger, debug_only=False)¶
@log(logger): A decorator that logs the entry, exit, and exceptions of a function.
- Parameters:
_func (callable, optional) – The function to be decorated. If None, the decorator is returned.
logger (logging.Logger) – The logger instance used for logging.
debug_only (bool) – a flag to indicate whether the decorator is only to run in debug mode, default False
- Returns:
The decorated function or the decorator itself.
- Return type:
callable
The decorator logs: - Entry into the function with the function name. - Arguments passed to the function if the logging level is DEBUG. - The result returned by the function if the logging level is DEBUG. - Exit from the function with the function name.
Example usage:
At the level of the class definition, define a logger exactly as follows:
import logging from poriscope.utils.LogDecotrator import log class SomePlugin(MetaPlugin): logger = logging.getLogger(__name__) @log(logger=logger) def __init__(self): ...
Once this is done, you can decorate any member functions you want logged to the logfile like so:
@log(logger=logger) def my_function(self, ...): pass
All member methods of plugins should be decorated with this logger. Calls to the logger inside functions proceed normally.
@log(logger=logger) def my_function(self, ...): self.logger.info('This message is informational')