Skip to content

handlers

JsonFileHandler

Bases: FileHandler

A log handler to output to a file.

Source code in m/log/handlers.py
class JsonFileHandler(logging.FileHandler):
    """A log handler to output to a file."""

    def __init__(self, formatter: logging.Formatter, filename: str):
        """Initialize the handler.

        Args:
            formatter: The formatter to use with the handler.
            filename: The file where to store the log recrods.
        """
        super().__init__(filename, encoding='UTF-8')
        self.setFormatter(formatter)

    def filter(self, _record: logging.LogRecord) -> bool:
        """Display all records.

        Args:
            _record: A logging record instance.

        Returns:
            True
        """
        return True

__init__(formatter, filename)

Initialize the handler.

Parameters:

Name Type Description Default
formatter Formatter

The formatter to use with the handler.

required
filename str

The file where to store the log recrods.

required
Source code in m/log/handlers.py
def __init__(self, formatter: logging.Formatter, filename: str):
    """Initialize the handler.

    Args:
        formatter: The formatter to use with the handler.
        filename: The file where to store the log recrods.
    """
    super().__init__(filename, encoding='UTF-8')
    self.setFormatter(formatter)

filter(_record)

Display all records.

Parameters:

Name Type Description Default
_record LogRecord

A logging record instance.

required

Returns:

Type Description
bool

True

Source code in m/log/handlers.py
def filter(self, _record: logging.LogRecord) -> bool:
    """Display all records.

    Args:
        _record: A logging record instance.

    Returns:
        True
    """
    return True

StdErrHandler

Bases: StreamHandler

A log handler to print to stderr.

Source code in m/log/handlers.py
class StdErrHandler(logging.StreamHandler):
    """A log handler to print to stderr."""

    def __init__(self, formatter: logging.Formatter):
        """Initialize the handler.

        Args:
            formatter: The formatter to use with the handler.
        """
        super().__init__(sys.stderr)
        self.setFormatter(formatter)

    def filter(self, record: logging.LogRecord) -> bool:
        """Only display warnings or error records.

        Args:
            record: A logging record instance.

        Returns:
            True if the record is a warning or error.
        """
        return record.levelno >= logging.WARNING

__init__(formatter)

Initialize the handler.

Parameters:

Name Type Description Default
formatter Formatter

The formatter to use with the handler.

required
Source code in m/log/handlers.py
def __init__(self, formatter: logging.Formatter):
    """Initialize the handler.

    Args:
        formatter: The formatter to use with the handler.
    """
    super().__init__(sys.stderr)
    self.setFormatter(formatter)

filter(record)

Only display warnings or error records.

Parameters:

Name Type Description Default
record LogRecord

A logging record instance.

required

Returns:

Type Description
bool

True if the record is a warning or error.

Source code in m/log/handlers.py
def filter(self, record: logging.LogRecord) -> bool:
    """Only display warnings or error records.

    Args:
        record: A logging record instance.

    Returns:
        True if the record is a warning or error.
    """
    return record.levelno >= logging.WARNING

StdOutHandler

Bases: StreamHandler

A log handler to print to stdout.

Source code in m/log/handlers.py
class StdOutHandler(logging.StreamHandler):
    """A log handler to print to stdout."""

    def __init__(self, formatter: logging.Formatter):
        """Initialize the handler.

        Args:
            formatter: The formatter to use with the handler.
        """
        super().__init__(sys.stdout)
        self.setFormatter(formatter)

    def filter(self, record: logging.LogRecord) -> bool:
        """Display non-warnings and non-error records.

        Args:
            record: A logging record instance.

        Returns:
            True if the record is not a warning or error.
        """
        return record.levelno < logging.WARNING

__init__(formatter)

Initialize the handler.

Parameters:

Name Type Description Default
formatter Formatter

The formatter to use with the handler.

required
Source code in m/log/handlers.py
def __init__(self, formatter: logging.Formatter):
    """Initialize the handler.

    Args:
        formatter: The formatter to use with the handler.
    """
    super().__init__(sys.stdout)
    self.setFormatter(formatter)

filter(record)

Display non-warnings and non-error records.

Parameters:

Name Type Description Default
record LogRecord

A logging record instance.

required

Returns:

Type Description
bool

True if the record is not a warning or error.

Source code in m/log/handlers.py
def filter(self, record: logging.LogRecord) -> bool:
    """Display non-warnings and non-error records.

    Args:
        record: A logging record instance.

    Returns:
        True if the record is not a warning or error.
    """
    return record.levelno < logging.WARNING