Skip to content
Advertisement

How to set logging level in twisted?

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?

Updated.

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)

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

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

logging.basicConfig didn’t help.

Advertisement

Answer

I found the solution:

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)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement