Skip to content

log

trestle.common.log ¤

Common logging utilities.

Classes¤

SpecificLevelFilter (Filter) ¤

Filter for the same level as provided by setLevel for a log handler.

Python by default logs all levels above to a given destination. This makes it easy to split levels where you might log all levels to file and only errors to std.err, however, does not allow logging a specific level elsewhere.

Source code in trestle/common/log.py
class SpecificLevelFilter(logging.Filter):
    """
    Filter for the same level as provided by setLevel for a log handler.

    Python by default logs all levels above to a given destination. This makes it easy to split levels where you might
    log all levels to file and only errors to std.err, however, does not allow logging a specific level elsewhere.
    """

    def __init__(self, level: int) -> None:
        """Initialize providing maximum level to be pushed through the filter."""
        self._level = level

    def filter(self, log_record: logging.LogRecord) -> bool:  # noqa: A003
        """Filter log messages."""
        return log_record.levelno == self._level
Methods¤
__init__(self, level) special ¤

Initialize providing maximum level to be pushed through the filter.

Source code in trestle/common/log.py
def __init__(self, level: int) -> None:
    """Initialize providing maximum level to be pushed through the filter."""
    self._level = level
filter(self, log_record) ¤

Filter log messages.

Source code in trestle/common/log.py
def filter(self, log_record: logging.LogRecord) -> bool:  # noqa: A003
    """Filter log messages."""
    return log_record.levelno == self._level

Trace ¤

Class allowing low priority trace message when verbose > 1 and log level below DEBUG.

Source code in trestle/common/log.py
class Trace():
    """Class allowing low priority trace message when verbose > 1 and log level below DEBUG."""

    def __init__(self, logger: logging.Logger) -> None:
        """Store the main logger with its module name."""
        self._logger = logger

    def log(self, msg: str) -> None:
        """Output the trace msg if log level is below DEBUG."""
        level = self._logger.getEffectiveLevel()
        if level < logging.DEBUG:
            self._logger.debug(msg)
Methods¤
__init__(self, logger) special ¤

Store the main logger with its module name.

Source code in trestle/common/log.py
def __init__(self, logger: logging.Logger) -> None:
    """Store the main logger with its module name."""
    self._logger = logger
log(self, msg) ¤

Output the trace msg if log level is below DEBUG.

Source code in trestle/common/log.py
def log(self, msg: str) -> None:
    """Output the trace msg if log level is below DEBUG."""
    level = self._logger.getEffectiveLevel()
    if level < logging.DEBUG:
        self._logger.debug(msg)

Functions¤

get_current_verbosity_level(logger) ¤

Get the current verbosity level based on logging level.

Source code in trestle/common/log.py
def get_current_verbosity_level(logger: logging.Logger) -> int:
    """Get the current verbosity level based on logging level."""
    level = logger.getEffectiveLevel()
    if level < logging.DEBUG:
        return 2
    elif level == logging.DEBUG:
        return 1
    return 0

set_global_logging_levels(level=20) ¤

Initialise logging.

Should only be invoked by the CLI classes or similar.

Source code in trestle/common/log.py
def set_global_logging_levels(level: int = logging.INFO) -> None:
    """Initialise logging.

    Should only be invoked by the CLI classes or similar.
    """
    # This line stops default root loggers setup for a python context from logging extra messages.
    # DO NOT USE THIS COMMAND directly from an SDK. Handle logs levels based on your own application
    _logger.propagate = False
    # Remove handlers
    _logger.handlers.clear()
    # set global level
    _logger.setLevel(level)
    # Create standard out
    console_out_handler = logging.StreamHandler(sys.stdout)
    console_out_handler.setLevel(logging.INFO)
    console_out_handler.addFilter(SpecificLevelFilter(logging.INFO))

    console_debug_handler = logging.StreamHandler(sys.stdout)
    console_debug_handler.setLevel(logging.DEBUG)
    console_debug_handler.addFilter(SpecificLevelFilter(logging.DEBUG))

    console_error_handler = logging.StreamHandler(sys.stderr)
    console_error_handler.setLevel(logging.WARNING)
    # create formatters
    error_formatter = logging.Formatter('%(name)s:%(lineno)d %(levelname)s: %(message)s')
    debug_formatter = logging.Formatter('%(name)s:%(lineno)d %(levelname)s: %(message)s')
    console_debug_handler.setFormatter(debug_formatter)
    console_error_handler.setFormatter(error_formatter)
    # add ch to logger
    _logger.addHandler(console_out_handler)
    _logger.addHandler(console_error_handler)
    _logger.addHandler(console_debug_handler)

set_log_level_from_args(args) ¤

Vanity function to automatically set log levels based on verbosity flags.

Source code in trestle/common/log.py
def set_log_level_from_args(args: argparse.Namespace) -> None:
    """Vanity function to automatically set log levels based on verbosity flags."""
    if args.verbose > 1:
        # these msgs only output by trace calls
        set_global_logging_levels(_get_trace_level())
    elif args.verbose == 1:
        set_global_logging_levels(logging.DEBUG)
    else:
        set_global_logging_levels(logging.INFO)
        sys.excepthook = _exception_handler

handler: python