Skip to content

Commit

Permalink
Simplify the example of dynamic handler level
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Dec 1, 2024
1 parent 2e0cd7b commit 7f5001f
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions docs/resources/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7f5001f

Please sign in to comment.