问题
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.
回答1:
To find all loggers and their corresponding names execute:
logging.Logger.manager.loggerDict.keys()
Try to set it explicitly by getting the noisy logger instance:
import logging
noisyLogger = logging.getLogger("autobahn") # adjust logger name
# noisyLogger.getEffectiveLevel() # indicates the current effective level
noisyLogger.setLevel(logging.INFO)
回答2:
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)
来源:https://stackoverflow.com/questions/49063649/how-to-set-logging-level-in-twisted