如何在双绞线中设置日志记录级别? [英] How to set logging level in twisted?

查看:87
本文介绍了如何在双绞线中设置日志记录级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用扭曲的高速公路包,它在每次连接到websocket时为我显示调试消息.我试图将日志记录级别切换为信息,但没有成功:

I am using autobahn package with twisted which shows debug message for me every connect to websocket. I tried to switch logging level to info but had no success:

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

是否有一种简便的方法来切换日志级别?

Is there an easy way to switch log level?

已更新.

这是twisted_service.py:

Here is the 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 -noy twisted_service.py 我收到一条消息:

I run it using twistd script: twistd -noy twisted_service.py And i get a message:

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

2018-03-03T10:45:22+0500 [builtin.WebsocketService#debug] start service

logging.basicConfig没有帮助.

logging.basicConfig didn't help.

推荐答案

我找到了解决方法:

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)

这篇关于如何在双绞线中设置日志记录级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆