Merge pull request #191 from guewen/13.0-log-user-id-uuid

[13.0] Extend json logger with request uuid and user id
This commit is contained in:
Guewen Baconnier
2020-06-22 13:36:59 +02:00
committed by GitHub
co-authored by GitHub
+17
View File
@@ -4,9 +4,12 @@
import logging
import os
import threading
import uuid
from distutils.util import strtobool
from odoo import http
_logger = logging.getLogger(__name__)
try:
@@ -25,6 +28,8 @@ 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)
record.uid = getattr(threading.current_thread(), 'uid', None)
_super = super(OdooJsonFormatter, self)
return _super.add_fields(log_record, record, message_dict)
@@ -34,3 +39,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