Skip to content

Logging handler

Module to configure project logging.

FilterSensitiveData

Bases: logging.Filter

Logging filter to hide sensitive data from being shown in the logs.

Source code in mkdocs/lakehouse_engine/packages/utils/logging_handler.py
class FilterSensitiveData(logging.Filter):
    """Logging filter to hide sensitive data from being shown in the logs."""

    def filter(self, record: logging.LogRecord) -> bool:  # noqa: A003
        """Hide sensitive information from being shown in the logs.

        Based on the configured regex and replace strings, the content of the log
        records is replaced and then all the records are allowed to be logged
        (return True).

        Args:
            record: the LogRecord event being logged.

        Returns:
            The transformed record to be logged.
        """
        for key_reg in SENSITIVE_KEYS_REG:
            record.msg = re.sub(key_reg["regex"], key_reg["replace"], str(record.msg))
        return True

filter(record)

Hide sensitive information from being shown in the logs.

Based on the configured regex and replace strings, the content of the log records is replaced and then all the records are allowed to be logged (return True).

Parameters:

Name Type Description Default
record logging.LogRecord

the LogRecord event being logged.

required

Returns:

Type Description
bool

The transformed record to be logged.

Source code in mkdocs/lakehouse_engine/packages/utils/logging_handler.py
def filter(self, record: logging.LogRecord) -> bool:  # noqa: A003
    """Hide sensitive information from being shown in the logs.

    Based on the configured regex and replace strings, the content of the log
    records is replaced and then all the records are allowed to be logged
    (return True).

    Args:
        record: the LogRecord event being logged.

    Returns:
        The transformed record to be logged.
    """
    for key_reg in SENSITIVE_KEYS_REG:
        record.msg = re.sub(key_reg["regex"], key_reg["replace"], str(record.msg))
    return True

LoggingHandler

Bases: object

Handle the logging of the lakehouse engine project.

Source code in mkdocs/lakehouse_engine/packages/utils/logging_handler.py
class LoggingHandler(object):
    """Handle the logging of the lakehouse engine project."""

    def __init__(self, class_name: str):
        """Construct a LoggingHandler instance.

        Args:
            class_name: name of the class to be indicated in the logs.
        """
        self._logger: logging.Logger = logging.getLogger(class_name)
        self._logger.setLevel(logging.DEBUG)
        self._logger.addFilter(FilterSensitiveData())
        lsh = logging.StreamHandler()
        lsh.setLevel(logging.DEBUG)
        lsh.setFormatter(FORMATTER)
        if not self._logger.hasHandlers():
            # avoid keep adding handlers and therefore duplicate messages
            self._logger.addHandler(lsh)

    def get_logger(self) -> logging.Logger:
        """Get the _logger instance variable.

        Returns:
            logging.Logger: the logger object.
        """
        return self._logger

__init__(class_name)

Construct a LoggingHandler instance.

Parameters:

Name Type Description Default
class_name str

name of the class to be indicated in the logs.

required
Source code in mkdocs/lakehouse_engine/packages/utils/logging_handler.py
def __init__(self, class_name: str):
    """Construct a LoggingHandler instance.

    Args:
        class_name: name of the class to be indicated in the logs.
    """
    self._logger: logging.Logger = logging.getLogger(class_name)
    self._logger.setLevel(logging.DEBUG)
    self._logger.addFilter(FilterSensitiveData())
    lsh = logging.StreamHandler()
    lsh.setLevel(logging.DEBUG)
    lsh.setFormatter(FORMATTER)
    if not self._logger.hasHandlers():
        # avoid keep adding handlers and therefore duplicate messages
        self._logger.addHandler(lsh)

get_logger()

Get the _logger instance variable.

Returns:

Type Description
logging.Logger

logging.Logger: the logger object.

Source code in mkdocs/lakehouse_engine/packages/utils/logging_handler.py
def get_logger(self) -> logging.Logger:
    """Get the _logger instance variable.

    Returns:
        logging.Logger: the logger object.
    """
    return self._logger