logging with filters

前端 未结 4 1836
难免孤独
难免孤独 2020-12-01 05:31

I\'m using Logging (import logging) to log messages.

Within 1 single module, I am logging messages at the debug level my_logger.debug(\'msg\')

4条回答
  •  孤独总比滥情好
    2020-12-01 05:39

    Just implement a subclass of logging.Filter: http://docs.python.org/library/logging.html#filter-objects. It will have one method, filter(record), that examines the log record and returns True to log it or False to discard it. Then you can install the filter on either a Logger or a Handler by calling its addFilter(filter) method.

    Example:

    class NoParsingFilter(logging.Filter):
        def filter(self, record):
            return not record.getMessage().startswith('parsing')
    
    logger.addFilter(NoParsingFilter())
    

    Or something like that, anyway.

提交回复
热议问题