Module panama.logging.decorators

Functions

def log_execution(logger_name: str | None = None, level: int = 20, blocking: bool = True)
Expand source code
def log_execution(logger_name: Union[str, None] = None, level: int = logging.INFO, blocking: bool = True):
    """Decorator function, used to retrieve the logger by logger name and log the operations of a function.

    Args:
        logger_name (Union[str, None], optional): name of the logger to retrieve. If None, the root logger is used. Defaults to None.
        level (int, optional): level of the log step. Defaults to panama_log.INFO.
        blocking (bool, optional): flag that indicates if the task must be closed and the error raised. Defaults to True.
    """

    def wrapper(func: Callable) -> Callable:
        @functools.wraps(func)
        def inner(*args: Any, **kwargs: Any) -> Any:
            # get status of the logging
            active_log = panama.logging.get_active_log()
            # if level logging is not activated, do not log stuff
            if active_log >= 99:
                return func(*args, **kwargs)

            # get the current context manager
            cm = get_context_manager()
            # get the current logger
            logger = panama.logging.get_logger(name=logger_name, context_manager=cm)
            # generate step name as the func name
            step_id = func.__name__

            # format logging args and kwargs
            extra = _keep_default_type_args_kwargs(*args, **kwargs)

            # try-catch for logging: warnings are handled
            try:
                result = func(*args, **kwargs)
                logger.log(level=level, msg=step_id, extra=extra)
                return result
            except Exception as e:
                if isinstance(e, Warning):
                    logger.warning(step_id, extra=extra, exc_info=e)
                else:
                    logger.error(step_id, extra, exc_info=e)
                    if blocking is True:
                        raise e

        return inner

    return wrapper

Decorator function, used to retrieve the logger by logger name and log the operations of a function.

Args

logger_name : Union[str, None], optional
name of the logger to retrieve. If None, the root logger is used. Defaults to None.
level : int, optional
level of the log step. Defaults to panama_log.INFO.
blocking : bool, optional
flag that indicates if the task must be closed and the error raised. Defaults to True.