From 45f37870af7644ad00a320bc0fe289a11e66f168 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 8 May 2024 13:33:59 -0400 Subject: [PATCH] Add a `.log.at_least_level()` predicate --- tractor/log.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/tractor/log.py b/tractor/log.py index e85b49c..f7d6a32 100644 --- a/tractor/log.py +++ b/tractor/log.py @@ -21,6 +21,11 @@ Log like a forester! from collections.abc import Mapping import sys import logging +from logging import ( + LoggerAdapter, + Logger, + StreamHandler, +) import colorlog # type: ignore import trio @@ -80,7 +85,7 @@ BOLD_PALETTE = { # TODO: this isn't showing the correct '{filename}' # as it did before.. -class StackLevelAdapter(logging.LoggerAdapter): +class StackLevelAdapter(LoggerAdapter): def transport( self, @@ -237,6 +242,7 @@ def get_logger( '''Return the package log or a sub-logger for ``name`` if provided. ''' + log: Logger log = rlog = logging.getLogger(_root_name) if ( @@ -291,7 +297,7 @@ def get_logger( def get_console_log( level: str | None = None, **kwargs, -) -> logging.LoggerAdapter: +) -> LoggerAdapter: '''Get the package logger and enable a handler which writes to stderr. Yeah yeah, i know we can use ``DictConfig``. You do it. @@ -316,7 +322,7 @@ def get_console_log( None, ) ): - handler = logging.StreamHandler() + handler = StreamHandler() formatter = colorlog.ColoredFormatter( LOG_FORMAT, datefmt=DATE_FORMAT, @@ -336,3 +342,19 @@ def get_loglevel() -> str: # global module logger for tractor itself log = get_logger('tractor') + + +def at_least_level( + log: Logger|LoggerAdapter, + level: int|str, +) -> bool: + ''' + Predicate to test if a given level is active. + + ''' + if isinstance(level, str): + level: int = LEVELS[level.upper()] + + if log.getEffectiveLevel() <= level: + return True + return False