From 071167d3d7661c3e7a6520d1cccf85ec9382aef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Mon, 29 Jan 2018 18:36:29 +0100 Subject: [PATCH] Refactor logging --- lib/config/logs.py | 5 +++++ src/app/app.py | 14 +++++++------- src/app/event_handler_connector.py | 9 +++++---- src/app/handlers/zmq_websocket_handler.py | 9 +++++---- src/event_handlers/terminado_event_handler.py | 5 +++-- src/event_handlers/terminado_server/server.py | 6 +++--- 6 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 lib/config/logs.py diff --git a/lib/config/logs.py b/lib/config/logs.py new file mode 100644 index 0000000..808287d --- /dev/null +++ b/lib/config/logs.py @@ -0,0 +1,5 @@ +import logging + +logging.getLogger().setLevel(logging.DEBUG) + +# TODO: configure supervisord loglevel from here diff --git a/src/app/app.py b/src/app/app.py index 8ee2815..0bc3fa1 100644 --- a/src/app/app.py +++ b/src/app/app.py @@ -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() diff --git a/src/app/event_handler_connector.py b/src/app/event_handler_connector.py index 5e9c466..f918aeb 100644 --- a/src/app/event_handler_connector.py +++ b/src/app/event_handler_connector.py @@ -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): diff --git a/src/app/handlers/zmq_websocket_handler.py b/src/app/handlers/zmq_websocket_handler.py index 1542280..c65a955 100644 --- a/src/app/handlers/zmq_websocket_handler.py +++ b/src/app/handlers/zmq_websocket_handler.py @@ -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): diff --git a/src/event_handlers/terminado_event_handler.py b/src/event_handlers/terminado_event_handler.py index 7c6546f..0fd9fb2 100644 --- a/src/event_handlers/terminado_event_handler.py +++ b/src/event_handlers/terminado_event_handler.py @@ -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? diff --git a/src/event_handlers/terminado_server/server.py b/src/event_handlers/terminado_server/server.py index ea0b924..7f8de5f 100644 --- a/src/event_handlers/terminado_server/server.py +++ b/src/event_handlers/terminado_server/server.py @@ -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()