本文整理汇总了Python中rainbow_logging_handler.RainbowLoggingHandler.addFilter方法的典型用法代码示例。如果您正苦于以下问题:Python RainbowLoggingHandler.addFilter方法的具体用法?Python RainbowLoggingHandler.addFilter怎么用?Python RainbowLoggingHandler.addFilter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rainbow_logging_handler.RainbowLoggingHandler
的用法示例。
在下文中一共展示了RainbowLoggingHandler.addFilter方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: enable_logging
# 需要导入模块: from rainbow_logging_handler import RainbowLoggingHandler [as 别名]
# 或者: from rainbow_logging_handler.RainbowLoggingHandler import addFilter [as 别名]
def enable_logging(filename: str, level: str):
class Squelch(logging.Filter):
def filter(self, record):
if record.levelno == logging.DEBUG:
return not record.name.startswith('asyncio') and \
not record.name.startswith('websockets') and \
not record.name.startswith('aiohttp')
elif record.levelno == logging.INFO and record.name.startswith('asyncio'):
return False
return True
formatter = logging.Formatter(fmt='%(asctime)s.%(msecs)03d:%(levelname)s:%(name)s:%(message)s')
# File logger captures everything.
file_handler = logging.FileHandler(filename)
file_handler.setLevel(logging.DEBUG)
# Console output level is configurable.
stream_handler = RainbowLoggingHandler(
sys.stdout,
color_asctime=('cyan', None, False),
color_msecs=('cyan', None, False),
color_levelname=('gray', None, False),
color_module=('yellow', None, False),
color_name=('blue', None, False),
color_lineno=('green', None, False),
datefmt="%Y-%m-%d %H:%M:%S"
)
stream_handler.setLevel(getattr(logging, level))
stream_handler.addFilter(Squelch())
# Set an output format.
stream_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
file_handler.addFilter(Squelch())
# Add handlers to root.
root = logging.getLogger('')
root.setLevel(logging.DEBUG)
root.addHandler(stream_handler)
root.addHandler(file_handler)