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:
JavaScript
x
3
1
import logging
2
logging.basicConfig(level=logging.INFO)
3
Is there an easy way to switch log level?
Updated.
Here is the twisted_service.py:
JavaScript
1
18
18
1
from twisted.application import service
2
from twisted.logger import Logger
3
import logging
4
logging.basicConfig(level=logging.INFO)
5
6
7
class WebsocketService(service.Service):
8
log = Logger()
9
10
def startService(self):
11
service.Service.startService(self)
12
self.log.debug('start service')
13
14
application = service.Application("ws")
15
16
ws_service = WebsocketService()
17
ws_service.setServiceParent(application)
18
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:
JavaScript
1
23
23
1
import sys
2
from twisted.application import service
3
from twisted.logger import LogLevelFilterPredicate, LogLevel
4
from twisted.logger import textFileLogObserver, FilteringLogObserver
5
6
7
class WebsocketService(service.Service):
8
log = Logger()
9
10
def startService(self):
11
service.Service.startService(self)
12
self.log.debug('start service')
13
14
application = service.Application("ws")
15
16
ws_service = WebsocketService()
17
ws_service.setServiceParent(application)
18
19
info_predicate = LogLevelFilterPredicate(LogLevel.info)
20
log_observer = FilteringLogObserver(textFileLogObserver(sys.stdout), predicates=info_predicate)
21
22
application.setComponent(ILogObserver, log_observer)
23