diff --git a/logging_json/README.rst b/logging_json/README.rst index 4d60884..cb07dd2 100644 --- a/logging_json/README.rst +++ b/logging_json/README.rst @@ -12,3 +12,7 @@ The json logging is activated with the environment variable In order to have the logs from the start of the server, you should add ``logging_json`` in the ``--load`` flag or in the ``server_wide_modules`` option in the configuration file. + +When the module is loaded, each HTTP request is tagged with a unique +``request_id`` so all logs produced while handling that request can be +correlated. diff --git a/logging_json/json_log.py b/logging_json/json_log.py index 3fcc796..8441667 100644 --- a/logging_json/json_log.py +++ b/logging_json/json_log.py @@ -3,9 +3,12 @@ import logging import os import threading +import uuid from distutils.util import strtobool +import openerp.http as http + _logger = logging.getLogger(__name__) try: @@ -24,12 +27,25 @@ class OdooJsonFormatter(jsonlogger.JsonFormatter): def add_fields(self, log_record, record, message_dict): record.pid = os.getpid() record.dbname = getattr(threading.currentThread(), 'dbname', '?') + record.request_id = getattr(threading.currentThread(), 'request_uuid', None) + record.perf_info = getattr(record, 'perf_info', '') _super = super(OdooJsonFormatter, self) return _super.add_fields(log_record, record, message_dict) if is_true(os.environ.get('ODOO_LOGGING_JSON')): format = ('%(asctime)s %(pid)s %(levelname)s' - '%(dbname)s %(name)s: %(message)s') + '%(dbname)s %(request_id)s %(name)s: %(message)s %(perf_info)s') formatter = OdooJsonFormatter(format) logging.getLogger().handlers[0].formatter = formatter + + +_original_webrequest_init = http.WebRequest.__init__ + + +def new_webrequest_init(self, httprequest): + _original_webrequest_init(self, httprequest) + threading.currentThread().request_uuid = uuid.uuid4().hex + + +http.WebRequest.__init__ = new_webrequest_init