Just for the record. NullHandler fits perfectly but two details:
1. it ignores filter function, because it has .blackhole = True
attribute, which triggers special branch in record dispatcher.
2. all handlers consume records of their *or higher* level. While it
would make more sense to silence records with level of handler *or less*
importance.
So i solved both problems by writing a custom handler that looks mostly
like this:
class NoopLessLevelHandler(logbook.Handler):
"""Like NullHandler, but not marked as blackhole, so usual
filter-handle-bubble logic is still applied.
Also, it only handles records with *less* level, as opposite
to default behavior.
Use this handler to temporarily silence e.g. channel "db"
with levels INFO and lower (DEBUG, NOTSET).
With bubble=True this handler does not change anything.
Usage:
>>> log = logbook.Logger("foo")
>>> with NoopLessLevelHandler(level=logbook.INFO, filter=lambda r, h:
r.channel == "foo").threadbound():
... log.info("this will be emited")
... log.debug("this will be dropped")
>>> log.info("this will be emited")
"""
def should_handle(self, record):
"""Opposite of default: handles records of less importance.
"""
return record.level < self.level
def handle(self, record):
"""Shortcut no-op handle.
"""
return True
Kudos to authors of logbook for easy to read and understand code. Stack
design is nice, have not yet encountered a problem with it.
--
You received this message because you are subscribed to the Google Groups
"pocoo-libs" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/pocoo-libs/-/MAGMeSVZ85wJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pocoo-libs?hl=en.