我正在使用 websockets
在python中创建一个websocket服务器的包;同时我大量使用 logging
在不同的日志级别。websockets
documentation提到记录器配置,它可以更改为日志级别 ERROR
使用
logger = logging.getLogger('websockets.server')
logger.setLevel(logging.ERROR)
logger.addHandler(logging.StreamHandler())
然而,这对我的代码没有任何影响,无论我把它放在哪里(在导入下方,在
websockets
导入之前,在 __main__
内)。我想要两种配置 - 一种全局日志配置和 websockets 服务器的记录器配置。另一种选择是完全禁用 websockets 模块的日志记录,但我不知道如何做到这一点。有任何想法吗?
最佳答案
原来它不仅是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)
关于python-3.x - 在 Python 中禁用 websockets 包的记录器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52281912/