From 7f5001fe46393627ced287bba2c4064442c3fd25 Mon Sep 17 00:00:00 2001 From: Delgan Date: Sun, 1 Dec 2024 12:17:44 +0100 Subject: [PATCH] Simplify the example of dynamic handler level --- docs/resources/recipes.rst | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/docs/resources/recipes.rst b/docs/resources/recipes.rst index 857683aa..3f6a7f3d 100644 --- a/docs/resources/recipes.rst +++ b/docs/resources/recipes.rst @@ -178,23 +178,20 @@ Alternatively, you can combine the |bind| method with the ``filter`` argument to Finally, more advanced control over handler's level can be achieved by using a callable object as the ``filter``:: - class MyFilter: + min_level = logger.level("DEBUG").no - def __init__(self, level): - self.level = level + def filter_by_level(record): + return record["level"].no >= min_level - def __call__(self, record): - levelno = logger.level(self.level).no - return record["level"].no >= levelno - my_filter = MyFilter("WARNING") - logger.add(sys.stderr, filter=my_filter, level=0) + logger.remove() + logger.add(sys.stderr, filter=filter_by_level, level=0) + + logger.debug("Logged") - logger.warning("OK") - logger.debug("NOK") + min_level = logger.level("WARNING").no - my_filter.level = "DEBUG" - logger.debug("OK") + logger.debug("Not logged") Configuring Loguru to be used by a library or an application