mirror of
https://github.com/camptocamp/odoo-cloud-platform.git
synced 2026-10-21 13:31:19 +00:00
imp: add perf info and request id in logs
This commit is contained in:
1 parent
772cdf75f4
commit
85566a51a5
2 files changed
+21
-1
No files matched your search
@@ -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.
|
||||
@@ -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
|
||||