From 1d5355c771c1a3d9e5bf1500720865726e9df3c0 Mon Sep 17 00:00:00 2001 From: boyska Date: Tue, 22 Apr 2014 16:32:55 +0200 Subject: [PATCH] Access-log fully customizable It is hidden by default, but TRANSLOGGER_OPTS can do many things for you --- server/default_config.py | 9 ++++++++- server/server.py | 22 +++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/server/default_config.py b/server/default_config.py index 743f69c..e95714e 100644 --- a/server/default_config.py +++ b/server/default_config.py @@ -1,6 +1,13 @@ +import logging HOST = 'localhost' PORT = '8000' -WSGI_SERVER = 'paste' +# pastelog is just "paste", but customized to accept logging options +WSGI_SERVER = 'pastelog' +# these are pastelog-specific options for logging engine +TRANSLOGGER_OPTS = { + 'logger_name': 'accesslog', + 'set_logger_level': logging.WARNING, + 'setup_console_handler': False } WSGI_SERVER_OPTIONS = {} DEBUG = True diff --git a/server/server.py b/server/server.py index 40bdeb0..98a09b9 100644 --- a/server/server.py +++ b/server/server.py @@ -6,13 +6,12 @@ logger = logging.getLogger('server') from functools import partial from bottle import Bottle, request, static_file, redirect, abort, response +import bottle botlog = logging.getLogger('bottle') botlog.setLevel(logging.INFO) botlog.addHandler(logging.StreamHandler(sys.stdout)) -import bottle bottle._stderr = lambda x: botlog.info(x.strip()) -del bottle from techrec import Rec, RecDB from processqueue import get_process_queue @@ -330,6 +329,16 @@ class DebugAPI(Bottle): /big/ : returns a 2**n -1 byte content ''' +class PasteLoggingServer(bottle.PasteServer): + def run(self, handler): # pragma: no cover + from paste import httpserver + from paste.translogger import TransLogger + handler = TransLogger(handler, **self.options['translogger_opts']) + del self.options['translogger_opts'] + httpserver.serve(handler, host=self.host, port=str(self.port), + **self.options) +bottle.server_names['pastelog'] = PasteLoggingServer + def main_cmd(*args): """meant to be called from argparse""" @@ -339,10 +348,17 @@ def main_cmd(*args): if get_config()['DEBUG']: c._app.mount('/debug', DebugAPI()) - c._app.run(server=get_config()['WSGI_SERVER'], + server = get_config()['WSGI_SERVER'] + if server == 'pastelog': + from paste.translogger import TransLogger + get_config()['WSGI_SERVER_OPTIONS']['translogger_opts'] = \ + get_config()['TRANSLOGGER_OPTS'] + + c._app.run(server=server, host=get_config()['HOST'], port=get_config()['PORT'], debug=get_config()['DEBUG'], + quiet=True, # this is to hide access.log style messages **get_config()['WSGI_SERVER_OPTIONS'] )