From 63a852da581bd58975f021ef1adda07312183881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Smodi=C5=A1?= Date: Mon, 5 Oct 2015 17:50:23 +0200 Subject: Add logging to file to python server. Configure logfile paths from environment variables CODEQ_SERVER_LOG and CODEQ_WEB_LOG. --- server/handlers.py | 15 ++++++++------- server/problems.py | 3 ++- server/socket.py | 8 ++++---- 3 files changed, 14 insertions(+), 12 deletions(-) (limited to 'server') diff --git a/server/handlers.py b/server/handlers.py index 99b021f..0a77e66 100644 --- a/server/handlers.py +++ b/server/handlers.py @@ -4,6 +4,7 @@ from concurrent.futures import ThreadPoolExecutor import traceback from errors.session import * import server +import logging class CodeqService(object): @@ -311,15 +312,15 @@ _executor = ThreadPoolExecutor(max_workers=100) def _invoke_handler(handler, request): try: - print('Worker thread processing data={}'.format(str(request.data))) + logging.debug('Worker thread processing data={}'.format(str(request.data))) handler.process(request) if not request.is_finished: - print('ERROR: the request was not concluded!') + logging.error('ERROR: the request was not concluded!') request.reply({'code': -1, 'message': 'Request processing did not provide a reply'}) - print('Processing finished') + logging.debug('Processing finished') except Exception as e: - print('ERROR: data processing failed: ' + str(e)) - traceback.print_exc() + logging.critical('ERROR: data processing failed: ' + str(e)) + logging.critical(traceback.format_exc()) request.reply({'code': -1, 'message': 'Internal error: ' + str(e)}) def serve_request(json_obj): @@ -335,7 +336,7 @@ def serve_request(json_obj): handler = incoming_handlers.get(action) if handler is None: raise RequestProcessingError('No handler for ' + action) - print("Attempting to serve action={}".format(action)) + logging.debug("Attempting to serve action={}".format(action)) session = None if sid is None: if not handler.session_is_optional: @@ -351,7 +352,7 @@ def serve_request(json_obj): def send(tid, sid, json_obj): # just a proxy function for now - print('Sending reply: {}'.format(str(json_obj))) + logging.debug('Sending reply: {}'.format(str(json_obj))) server.socket.sendPacket(tid, sid, json_obj) def stop(): diff --git a/server/problems.py b/server/problems.py index 220a746..bc124ca 100644 --- a/server/problems.py +++ b/server/problems.py @@ -5,6 +5,7 @@ import importlib.machinery import threading import os.path from db import get_connection, return_connection +import logging _path_prefix = os.environ.get('CODEQ_PROBLEMS') or '/var/local/codeq-problems' _module_loading_lock = threading.RLock() # TODO: make a more fine-grained locking import machinery @@ -19,7 +20,7 @@ def load_module(fullname): ff = importlib.machinery.FileFinder(d, (importlib.machinery.SourceFileLoader, ['.py'])) spec = ff.find_spec(fullname) if spec is None: - print('ERROR: there is no problem module {0}'.format(fullname)) + logging.error('ERROR: there is no problem module {0}'.format(fullname)) return None mod = type(sys)(fullname) mod.__loader__ = spec.loader diff --git a/server/socket.py b/server/socket.py index 121c6a4..bdf99bf 100644 --- a/server/socket.py +++ b/server/socket.py @@ -6,9 +6,9 @@ import json import threading import traceback from server.handlers import serve_request +import logging # TODO: add a whole lot of try..except blocks, and just make it overall error resistant -# TODO: add logging _mapping_lock = threading.Lock() # the lock guarding access to the two mappings below @@ -18,7 +18,7 @@ _transactions_to_socket = {} # keyed by tid, used only when there is no sid ava def processIncomingPacket(receiving_socket, packet): - print('Decoding JSON: {}'.format(packet)) + logging.debug('Decoding JSON: {}'.format(packet)) obj = json.loads(packet) req_type = obj.get('type') # private (meta) requests have the 'type' if req_type == 'connect': @@ -125,7 +125,7 @@ class JsonClientSocket(SocketHandler): self.destroy() else: # packet decode loop - print("received: {}".format(data)) + logging.debug("received: {}".format(data)) offset = 0 N = len(data) while (offset < N): @@ -160,7 +160,7 @@ class JsonClientSocket(SocketHandler): processIncomingPacket(self, s.decode('utf-8')) except Exception as e: # any exception that propagates to here means a possible protocol error, we have to disconnect - traceback.print_exc() + logging.critical(traceback.format_exc()) self.destroy() return else: -- cgit v1.2.1