From b4c341f5ae213f66e2faecab4004b9f71c7ec04a Mon Sep 17 00:00:00 2001 From: Xavier-Do Date: Mon, 3 Jul 2023 13:38:36 +0200 Subject: [PATCH] [IMP] runbot: log user beside ip --- runbot/__init__.py | 20 ++++++++++++++++++++ runbot/__manifest__.py | 4 ++-- runbot/models/__init__.py | 1 + runbot/models/ir_http.py | 14 ++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 runbot/models/ir_http.py diff --git a/runbot/__init__.py b/runbot/__init__.py index 718604bc..3d052492 100644 --- a/runbot/__init__.py +++ b/runbot/__init__.py @@ -5,3 +5,23 @@ from . import models from . import common from . import container from . import wizards + +import logging +import threading +from odoo.http import request + +class UserFilter(logging.Filter): + def filter(self, record): # noqa: A003 + message_parts = record.msg.split(' ', 2) + if message_parts[1] == '-': + uid = getattr(threading.current_thread(), 'uid', None) + if uid is None: + return True + user_name = getattr(threading.current_thread(), 'user_name', 'user') + message_parts[1] = f'({user_name}:{uid})' + record.msg = ' '.join(message_parts) + return True + + +def runbot_post_load(): + logging.getLogger('werkzeug').addFilter(UserFilter()) diff --git a/runbot/__manifest__.py b/runbot/__manifest__.py index 5125a8ca..51cfb3bf 100644 --- a/runbot/__manifest__.py +++ b/runbot/__manifest__.py @@ -77,6 +77,6 @@ '/web/static/lib/bootstrap/js/dist/collapse.js', '/runbot/static/src/js/runbot.js', ], - } - + }, + 'post_load': 'runbot_post_load', } diff --git a/runbot/models/__init__.py b/runbot/models/__init__.py index 3cec81e4..8fee1421 100644 --- a/runbot/models/__init__.py +++ b/runbot/models/__init__.py @@ -15,6 +15,7 @@ from . import dockerfile from . import event from . import host from . import ir_cron +from . import ir_http from . import ir_qweb from . import project from . import repo diff --git a/runbot/models/ir_http.py b/runbot/models/ir_http.py new file mode 100644 index 00000000..940fd38f --- /dev/null +++ b/runbot/models/ir_http.py @@ -0,0 +1,14 @@ +from odoo import models +from odoo.http import request +import threading + + +class IrHttp(models.AbstractModel): + _inherit = ["ir.http"] + + @classmethod + def _dispatch(cls, endpoint): + result = super()._dispatch(endpoint) + if request: + threading.current_thread().user_name = request.env.user.name + return result