在 Python 中禁用 websockets 包的记录器

mar*_*tin 4 logging websocket python-3.x

我正在使用该websockets包在 python 中创建一个 websocket 服务器;同时我logging在不同的日志级别中大量使用。

websockets 文档ERROR提到了记录器配置,并且可以使用以下命令将其更改为日志级别

     logger = logging.getLogger('websockets.server')
     logger.setLevel(logging.ERROR)
     logger.addHandler(logging.StreamHandler())
Run Code Online (Sandbox Code Playgroud)

然而,这对我的代码没有任何影响,无论我将其放置在何处(在导入下方、导入之前websockets、在 内__main__)。我想要两种配置 - 一种是全局日志记录配置,另一种是 websockets 服务器的记录器配置。

另一种选择是完全禁用 websockets 模块的日志记录,但我不知道如何做到这一点。有任何想法吗?

mar*_*tin 5

事实证明,不仅websockets.server发出大量调试消息,还有其他消息。我最终通过使用%(name)s根记录器配置中的字段来寻找这些包,并且可以找到websockets.protocol负责附加消息的人。

logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s %(name)s %(levelname)-8s  %(message)s',
    datefmt='(%H:%M:%S)')

# disable all loggers from different files
logging.getLogger('asyncio').setLevel(logging.ERROR)
logging.getLogger('asyncio.coroutines').setLevel(logging.ERROR)
logging.getLogger('websockets.server').setLevel(logging.ERROR)
logging.getLogger('websockets.protocol').setLevel(logging.ERROR)
Run Code Online (Sandbox Code Playgroud)