From fb91fac6f04afe9f2ae3668620dc6bf18aec7eba Mon Sep 17 00:00:00 2001 From: Marcin Kuzminski Date: Thu, 7 Sep 2017 17:56:52 +0200 Subject: [PATCH] gunicorn: allow custom logger to set consistent formatting of requests for gunicorn with rhodecode logging. --- configs/gunicorn_config.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/configs/gunicorn_config.py b/configs/gunicorn_config.py index e9599f82..bf6017ab 100644 --- a/configs/gunicorn_config.py +++ b/configs/gunicorn_config.py @@ -15,8 +15,11 @@ available post the .ini config. import multiprocessing import sys +import time +import datetime import threading import traceback +from gunicorn.glogging import Logger # GLOBAL @@ -87,4 +90,24 @@ def pre_request(worker, req): def post_request(worker, req, environ, resp): return worker.log.debug("[<%-10s>] POST WORKER: %s %s resp: %s", worker.pid, - req.method, req.path, resp.status_code) \ No newline at end of file + req.method, req.path, resp.status_code) + + +class RhodeCodeLogger(Logger): + """ + Custom Logger that allows some customization that gunicorn doesn't allow + """ + + datefmt = r"%Y-%m-%d %H:%M:%S" + + def __init__(self, cfg): + Logger.__init__(self, cfg) + + def now(self): + """ return date in RhodeCode Log format """ + now = time.time() + msecs = int((now - long(now)) * 1000) + return time.strftime(self.datefmt, time.localtime(now)) + '.{0:03d}'.format(msecs) + + +logger_class = RhodeCodeLogger