From c3b061e980ade78b7974b18ed0f7c8958c572249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Thu, 18 Jun 2020 19:37:56 +0200 Subject: [PATCH 1/2] Log request uuid --- logging_json/json_log.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/logging_json/json_log.py b/logging_json/json_log.py index 6bff4c7..f3cdf51 100644 --- a/logging_json/json_log.py +++ b/logging_json/json_log.py @@ -2,9 +2,12 @@ import logging import os import threading +import uuid from distutils.util import strtobool +from odoo import http + _logger = logging.getLogger(__name__) try: @@ -23,6 +26,7 @@ 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.current_thread(), 'request_uuid', None) _super = super(OdooJsonFormatter, self) return _super.add_fields(log_record, record, message_dict) @@ -32,3 +36,15 @@ if is_true(os.environ.get('ODOO_LOGGING_JSON')): '%(dbname)s %(name)s: %(message)s') formatter = OdooJsonFormatter(format) logging.getLogger().handlers[0].formatter = formatter + + +# monkey patch WebRequest constructor to store request_uuid +org_init = http.WebRequest.__init__ + + +def new_init(self, httprequest): + org_init(self, httprequest) + threading.current_thread().request_uuid = uuid.uuid4() + + +http.WebRequest.__init__ = new_init From 384c75061af274c17467111b2c0bdf9062553d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Thu, 18 Jun 2020 19:38:08 +0200 Subject: [PATCH 2/2] Log uid --- logging_json/json_log.py | 1 + 1 file changed, 1 insertion(+) diff --git a/logging_json/json_log.py b/logging_json/json_log.py index f3cdf51..3aceaf1 100644 --- a/logging_json/json_log.py +++ b/logging_json/json_log.py @@ -27,6 +27,7 @@ class OdooJsonFormatter(jsonlogger.JsonFormatter): record.pid = os.getpid() record.dbname = getattr(threading.currentThread(), 'dbname', '?') record.request_id = getattr(threading.current_thread(), 'request_uuid', None) + record.uid = getattr(threading.current_thread(), 'uid', None) _super = super(OdooJsonFormatter, self) return _super.add_fields(log_record, record, message_dict)