如何设置扭曲的日志级别?

问题描述 投票:1回答:2

我正在使用带有twisted的autobahn软件包,每次连接到websocket都显示调试消息。我试图将日志记录级别切换为info但没有成功:

import logging
logging.basicConfig(level=logging.INFO)

有没有简单的方法来切换日志级别?

更新。

这是twisted_service.py:

from twisted.application import service
from twisted.logger import Logger
import logging
logging.basicConfig(level=logging.INFO)


class WebsocketService(service.Service):
    log = Logger()

    def startService(self):
        service.Service.startService(self)
        self.log.debug('start service')

application = service.Application("ws")

ws_service = WebsocketService()
ws_service.setServiceParent(application)

我使用twistd脚本运行它:twistd -noy twisted_service.py然后我收到一条消息:

2018-03-03T10:45:22 + 0500 [builtin.WebsocketService#debug]启动服务

logging.basicConfig没有帮助。

python logging twisted autobahn
2个回答
2
投票

要查找所有记录器及其对应的名称,请执行:

logging.Logger.manager.loggerDict.keys()

尝试通过获取嘈杂的记录器实例来明确设置它:

import logging

noisyLogger = logging.getLogger("autobahn")  # adjust logger name
# noisyLogger.getEffectiveLevel()  # indicates the current effective level
noisyLogger.setLevel(logging.INFO)

1
投票

我找到了解决方案:

import sys
from twisted.application import service
from twisted.logger import LogLevelFilterPredicate, LogLevel
from twisted.logger import textFileLogObserver, FilteringLogObserver


class WebsocketService(service.Service):
    log = Logger()

    def startService(self):
        service.Service.startService(self)
        self.log.debug('start service')

application = service.Application("ws")

ws_service = WebsocketService()
ws_service.setServiceParent(application)

info_predicate = LogLevelFilterPredicate(LogLevel.info)
log_observer = FilteringLogObserver(textFileLogObserver(sys.stdout), predicates=info_predicate)

application.setComponent(ILogObserver, log_observer)
© www.soinside.com 2019 - 2024. All rights reserved.