Refactor logging

This commit is contained in:
Kristóf Tóth 2018-01-29 18:36:29 +01:00
parent 1856831d80
commit 071167d3d7
6 changed files with 28 additions and 20 deletions

5
lib/config/logs.py Normal file
View File

@ -0,0 +1,5 @@
import logging
logging.getLogger().setLevel(logging.DEBUG)
# TODO: configure supervisord loglevel from here

View File

@ -1,4 +1,3 @@
import logging
import sys
import tornado
@ -7,6 +6,8 @@ from tornado.web import Application
from tornado.ioloop import IOLoop
from config import WEB_PORT
from config.logs import logging
log = logging.getLogger(__name__)
from handlers import FSMManagingSocketHandler
from sql_injection_fsm import SQLInjectionFSM
@ -26,10 +27,9 @@ if __name__ == '__main__':
)
application.listen(WEB_PORT)
logging.getLogger().setLevel(logging.DEBUG)
logging.debug('Python version: {}'.format(sys.version[:5]))
logging.debug('Tornado version: {}'.format(tornado.version))
logging.debug('ZeroMQ version: {}'.format(zmq.zmq_version()))
logging.debug('PyZMQ version: {}'.format(zmq.pyzmq_version()))
logging.info('Tornado application listening on port {}'.format(WEB_PORT))
log.debug('Python version: {}'.format(sys.version[:5]))
log.debug('Tornado version: {}'.format(tornado.version))
log.debug('ZeroMQ version: {}'.format(zmq.zmq_version()))
log.debug('PyZMQ version: {}'.format(zmq.pyzmq_version()))
log.info('Tornado application listening on port {}'.format(WEB_PORT))
IOLoop.instance().start()

View File

@ -1,12 +1,13 @@
import logging
import zmq
from zmq.eventloop import ioloop
from zmq.eventloop.zmqstream import ZMQStream
from config import PUBLISHER_PORT, RECEIVER_PORT
from config.logs import logging
log = logging.getLogger(__name__)
from util import parse_anchor_from_message, ZMQConnectorBase
ioloop.install()
@ -17,7 +18,7 @@ class EventHandlerDownlinkConnector(ZMQConnectorBase):
self._zmq_pull_stream = ZMQStream(self._zmq_pull_socket)
address = 'tcp://*:{}'.format(RECEIVER_PORT)
self._zmq_pull_socket.bind(address)
logging.debug('Pull socket bound to {}'.format(address))
log.debug('Pull socket bound to {}'.format(address))
class EventHandlerUplinkConnector(ZMQConnectorBase):
@ -26,7 +27,7 @@ class EventHandlerUplinkConnector(ZMQConnectorBase):
self._zmq_pub_socket = self._zmq_context.socket(zmq.PUB)
address = 'tcp://*:{}'.format(PUBLISHER_PORT)
self._zmq_pub_socket.bind(address)
logging.debug('Pub socket bound to {}'.format(address))
log.debug('Pub socket bound to {}'.format(address))
class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkConnector):

View File

@ -1,9 +1,10 @@
import logging
import json
from tornado.websocket import WebSocketHandler
from util import parse_anchor_from_message
from event_handler_connector import EventHandlerConnector
from config.logs import logging
log = logging.getLogger(__name__)
class ZMQWebSocketHandler(WebSocketHandler):
@ -13,16 +14,16 @@ class ZMQWebSocketHandler(WebSocketHandler):
super().__init__(application, request, **kwargs)
def open(self, *args, **kwargs):
logging.debug('WebSocket connection initiated')
log.debug('WebSocket connection initiated')
self._event_handler_connector.register_callback(self.zmq_callback)
def zmq_callback(self, msg_parts):
anchor, data = msg_parts
logging.debug('Received on pull socket: {}'.format(data.decode()))
log.debug('Received on pull socket: {}'.format(data.decode()))
self.write_message(data.decode())
def on_message(self, message):
logging.debug('Received on WebSocket: {}'.format(message))
log.debug('Received on WebSocket: {}'.format(message))
self.send_message(*self.make_response(message))
def make_response(self, message):

View File

@ -1,9 +1,10 @@
import logging
from shutil import rmtree, copytree
from event_handler_base import EventHandlerBase
from util import SupervisorMixin
from config import TERMINADO_DIR
from config.logs import logging
log = logging.getLogger(__name__)
class TerminadoEventHandler(EventHandlerBase, SupervisorMixin):
@ -19,5 +20,5 @@ class TerminadoEventHandler(EventHandlerBase, SupervisorMixin):
self.supervisor.startProcess(self.process_name)
def handle_event(self, anchor, data_json):
logging.debug('TerminadoEventHandler received event for anchor {}'.format(anchor))
log.debug('TerminadoEventHandler received event for anchor {}'.format(anchor))
# TODO: wat do?

View File

@ -1,9 +1,10 @@
import logging
from tornado.ioloop import IOLoop
from tornado.web import Application
from terminado import TermSocket, UniqueTermManager
from config import TERMINADO_PORT, TERMINADO_WD
from config.logs import logging
log = logging.getLogger(__name__)
class TerminadoMiniServer:
@ -28,6 +29,5 @@ class TerminadoMiniServer:
if __name__ == '__main__':
logging.getLogger().setLevel(logging.DEBUG)
logging.info('Terminado Mini Server listening on {}'.format(TERMINADO_PORT))
log.info('Terminado Mini Server listening on {}'.format(TERMINADO_PORT))
TerminadoMiniServer('/terminal', TERMINADO_PORT, TERMINADO_WD, ['bash']).listen()