From 16fa971623a85a5984dca169dbf62cbe3b3e4e2f Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Tue, 15 Jul 2025 10:33:50 +0200 Subject: [PATCH] [ADD] fastapi_log --- fastapi_log/README.rst | 99 +++++ fastapi_log/__init__.py | 2 + fastapi_log/__manifest__.py | 23 + fastapi_log/fastapi_dispatcher.py | 76 ++++ fastapi_log/models/__init__.py | 2 + fastapi_log/models/fastapi_endpoint.py | 35 ++ fastapi_log/models/fastapi_log.py | 227 ++++++++++ fastapi_log/readme/CONTRIBUTORS.md | 1 + fastapi_log/readme/DESCRIPTION.md | 3 + fastapi_log/readme/USAGE.md | 6 + fastapi_log/security/ir_model_access.xml | 17 + fastapi_log/security/res_groups.xml | 17 + fastapi_log/static/description/index.html | 438 +++++++++++++++++++ fastapi_log/tests/__init__.py | 1 + fastapi_log/tests/test_fastapi_log.py | 161 +++++++ fastapi_log/views/fastapi_endpoint_views.xml | 42 ++ fastapi_log/views/fastapi_log_views.xml | 124 ++++++ setup/fastapi_log/odoo/addons/fastapi_log | 1 + setup/fastapi_log/setup.py | 6 + 19 files changed, 1281 insertions(+) create mode 100644 fastapi_log/README.rst create mode 100644 fastapi_log/__init__.py create mode 100644 fastapi_log/__manifest__.py create mode 100644 fastapi_log/fastapi_dispatcher.py create mode 100644 fastapi_log/models/__init__.py create mode 100644 fastapi_log/models/fastapi_endpoint.py create mode 100644 fastapi_log/models/fastapi_log.py create mode 100644 fastapi_log/readme/CONTRIBUTORS.md create mode 100644 fastapi_log/readme/DESCRIPTION.md create mode 100644 fastapi_log/readme/USAGE.md create mode 100644 fastapi_log/security/ir_model_access.xml create mode 100644 fastapi_log/security/res_groups.xml create mode 100644 fastapi_log/static/description/index.html create mode 100644 fastapi_log/tests/__init__.py create mode 100644 fastapi_log/tests/test_fastapi_log.py create mode 100644 fastapi_log/views/fastapi_endpoint_views.xml create mode 100644 fastapi_log/views/fastapi_log_views.xml create mode 120000 setup/fastapi_log/odoo/addons/fastapi_log create mode 100644 setup/fastapi_log/setup.py diff --git a/fastapi_log/README.rst b/fastapi_log/README.rst new file mode 100644 index 000000000..6464683d2 --- /dev/null +++ b/fastapi_log/README.rst @@ -0,0 +1,99 @@ +=========== +Fastapi Log +=========== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:ef0c0bceb8ae27bcfebaebc22e2fb4747475f2a2c60dd2d410bc40b6efee9b6a + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github + :target: https://github.com/OCA/rest-framework/tree/16.0/fastapi_log + :alt: OCA/rest-framework +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-fastapi_log + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows an endpoint to activate full request logging in a +database model. + +It is useful to debug production issues or to monitor the usage of a +specific endpoint. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To activate logging for an endpoint, you have to check the +``Log Requests`` checkbox in the endpoint's configuration. This will log +all requests and responses for that endpoint. + +A smart button will be displayed in the endpoint's form view to access +the endpoint logs. A global log view is also available in the +``FastAPI Logs`` menu. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Akretion + +Contributors +------------ + +- Florian Mounier florian.mounier@akretion.com + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-paradoxxxzero| image:: https://github.com/paradoxxxzero.png?size=40px + :target: https://github.com/paradoxxxzero + :alt: paradoxxxzero + +Current `maintainer `__: + +|maintainer-paradoxxxzero| + +This module is part of the `OCA/rest-framework `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/fastapi_log/__init__.py b/fastapi_log/__init__.py new file mode 100644 index 000000000..d54296502 --- /dev/null +++ b/fastapi_log/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import fastapi_dispatcher diff --git a/fastapi_log/__manifest__.py b/fastapi_log/__manifest__.py new file mode 100644 index 000000000..c10eaf057 --- /dev/null +++ b/fastapi_log/__manifest__.py @@ -0,0 +1,23 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Fastapi Log", + "version": "16.0.1.0.0", + "author": "Akretion, Odoo Community Association (OCA)", + "summary": "Log Fastapi requests in database", + "category": "Tools", + "depends": ["fastapi"], + "website": "https://github.com/OCA/rest-framework", + "data": [ + "security/res_groups.xml", + "security/ir_model_access.xml", + "views/fastapi_endpoint_views.xml", + "views/fastapi_log_views.xml", + ], + "maintainers": ["paradoxxxzero"], + "demo": [], + "installable": True, + "license": "AGPL-3", +} diff --git a/fastapi_log/fastapi_dispatcher.py b/fastapi_log/fastapi_dispatcher.py new file mode 100644 index 000000000..3e0a68f26 --- /dev/null +++ b/fastapi_log/fastapi_dispatcher.py @@ -0,0 +1,76 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import logging + +from odoo import registry, tools +from odoo.http import _dispatchers + +from odoo.addons.fastapi.fastapi_dispatcher import ( + FastApiDispatcher as BaseFastApiDispatcher, +) + +_logger = logging.getLogger(__name__) + + +# Inherit from last registered fastapi dispatcher +# This handles multiple overload of dispatchers +class FastApiDispatcher(_dispatchers.get("fastapi", BaseFastApiDispatcher)): + routing_type = "fastapi" + + def dispatch(self, endpoint, args): + self.request.params = {} + environ = self._get_environ() + root_path = "/" + environ["PATH_INFO"].split("/")[1] + fastapi_endpoint = ( + self.request.env["fastapi.endpoint"] + .sudo() + .search([("root_path", "=", root_path)]) + ) + if fastapi_endpoint.log_requests: + log = None + try: + if tools.config["test_enable"]: + cr = getattr( + self.request.env.registry, "test_log_cr", self.request.env.cr + ) + else: + # Create an independent cursor + cr = registry(self.request.env.cr.dbname).cursor() + + env = self.request.env(cr=cr, su=True) + try: + # cf fastapi _get_environ + request = self.request.httprequest._HTTPRequest__wrapped + except AttributeError: + request = self.request.httprequest + + log = env["fastapi.log"].log_request( + request, environ, fastapi_endpoint.id + ) + except Exception as e: + _logger.warning("Failed to log request", exc_info=e) + + try: + response = super().dispatch(endpoint, args) + except Exception as e: + try: + log and log.log_exception(e) + except Exception as e: + _logger.warning("Failed to log exception", exc_info=e) + raise e + else: + try: + log and log.log_response(response) + except Exception as e: + _logger.warning("Failed to log response", exc_info=e) + finally: + if not tools.config["test_enable"]: + try: + cr.commit() # pylint: disable=E8102 + finally: + cr.close() + return response + + else: + return super().dispatch(endpoint, args) diff --git a/fastapi_log/models/__init__.py b/fastapi_log/models/__init__.py new file mode 100644 index 000000000..cddd4099d --- /dev/null +++ b/fastapi_log/models/__init__.py @@ -0,0 +1,2 @@ +from . import fastapi_endpoint +from . import fastapi_log diff --git a/fastapi_log/models/fastapi_endpoint.py b/fastapi_log/models/fastapi_endpoint.py new file mode 100644 index 000000000..e2649f812 --- /dev/null +++ b/fastapi_log/models/fastapi_endpoint.py @@ -0,0 +1,35 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class FastapiEndpoint(models.Model): + _inherit = "fastapi.endpoint" + + log_requests = fields.Boolean( + help="Log requests in database.", + ) + + fastapi_log_ids = fields.One2many( + "fastapi.log", + "endpoint_id", + string="Logs", + ) + + fastapi_log_count = fields.Integer( + compute="_compute_fastapi_log_count", + string="Logs Count", + ) + + @api.depends("fastapi_log_ids") + def _compute_fastapi_log_count(self): + data = self.env["fastapi.log"].read_group( + [("endpoint_id", "in", self.ids)], + ["endpoint_id"], + ["endpoint_id"], + ) + mapped_data = {m["endpoint_id"][0]: m["endpoint_id_count"] for m in data} + for record in self: + record.fastapi_log_count = mapped_data.get(record.id, 0) diff --git a/fastapi_log/models/fastapi_log.py b/fastapi_log/models/fastapi_log.py new file mode 100644 index 000000000..526d38213 --- /dev/null +++ b/fastapi_log/models/fastapi_log.py @@ -0,0 +1,227 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import base64 +import json +import time +from traceback import format_exception + +from starlette.exceptions import HTTPException as StarletteHTTPException +from werkzeug.exceptions import HTTPException as WerkzeugHTTPException + +from odoo import api, fields, models + + +class FastapiLog(models.Model): + _name = "fastapi.log" + _description = "Fastapi Log" + _order = "id desc" + + endpoint_id = fields.Many2one( + "fastapi.endpoint", + string="Endpoint", + required=True, + ondelete="cascade", + index=True, + ) + + # Request + request_url = fields.Char() + request_method = fields.Char() + request_headers = fields.Json() + request_body = fields.Binary(attachment=False) + request_date = fields.Datetime() + request_time = fields.Float() + + # Response + response_status_code = fields.Integer() + response_headers = fields.Json() + response_body = fields.Binary(attachment=False) + response_date = fields.Datetime() + response_time = fields.Float() + + stack_trace = fields.Text() + + # Derived fields + name = fields.Char(compute="_compute_name", store=True) + time = fields.Float(compute="_compute_time", store=True) + request_preview = fields.Text(compute="_compute_request_preview") + response_preview = fields.Text(compute="_compute_response_preview") + request_b64 = fields.Binary( + string="Request Content", compute="_compute_request_b64" + ) + response_b64 = fields.Binary( + string="Response Content", compute="_compute_response_b64" + ) + request_headers_preview = fields.Text(compute="_compute_headers_preview") + response_headers_preview = fields.Text(compute="_compute_headers_preview") + request_content_type = fields.Char( + compute="_compute_request_headers_derived", store=True + ) + request_content_length = fields.Integer( + compute="_compute_request_headers_derived", store=True + ) + referrer = fields.Char(compute="_compute_request_headers_derived", store=True) + response_content_type = fields.Char( + compute="_compute_response_headers_derived", store=True + ) + response_content_length = fields.Integer( + compute="_compute_response_headers_derived", store=True + ) + + def _headers_to_dict(self, headers): + try: + return {key.lower(): value for key, value in headers.items()} + except AttributeError: + return {} + + def _current_time(self): + return time.time_ns() / 1e9 + + @api.model + def log_request(self, request, environ, endpoint_id): + body = None + # Be careful to not consume the request body if it hasn't been wrapped + stream = environ.get("wsgi.input") + if stream and stream.seekable(): + body = stream.read() + stream.seek(0) + + return self.create( + { + "endpoint_id": endpoint_id, + "request_url": request.url, + "request_method": request.method, + "request_headers": self._headers_to_dict(request.headers), + "request_body": body, + "request_date": fields.Datetime.now(), + "request_time": self._current_time(), + } + ) + + @api.model + def log_response(self, response): + return self.write( + { + "response_status_code": response.status_code, + "response_headers": self._headers_to_dict(response.headers), + "response_body": response.data, + "response_date": fields.Datetime.now(), + "response_time": self._current_time(), + } + ) + + @api.model + def log_exception(self, exception): + self.write( + { + "stack_trace": "".join(format_exception(exception)), + } + ) + if isinstance(exception, StarletteHTTPException): + return self.write( + { + "response_status_code": exception.status_code, + "response_headers": self._headers_to_dict(exception.headers), + "response_body": exception.detail, + "response_date": fields.Datetime.now(), + "response_time": self._current_time(), + } + ) + if isinstance(exception, WerkzeugHTTPException): + return self.write( + { + "response_status_code": exception.code, + "response_headers": self._headers_to_dict(exception.get_headers()), + "response_body": exception.get_body(), + "response_date": fields.Datetime.now(), + "response_time": self._current_time(), + } + ) + try: + return self.log_response( + self.env.registry["ir.http"]._handle_error(exception) + ) + except Exception: + return self.write( + { + "response_status_code": 599, + "response_body": str(exception), + "response_date": fields.Datetime.now(), + "response_time": self._current_time(), + } + ) + + @api.depends("request_url", "request_method", "request_date") + def _compute_name(self): + for record in self: + record.name = ( + f"{record.request_date.isoformat()} - " + f"[{record.request_method} {record.request_url}" + ) + + @api.depends("request_time", "response_time") + def _compute_time(self): + for record in self: + if record.request_time and record.response_time: + record.time = record.response_time - record.request_time + else: + record.time = 0 + + @api.depends("request_headers") + def _compute_request_headers_derived(self): + for record in self: + headers = record.request_headers or {} + record.request_content_type = headers.get("content-type", "") + record.request_content_length = headers.get("content-length", 0) + record.referrer = headers.get("referer", "") + + @api.depends("response_headers") + def _compute_response_headers_derived(self): + for record in self: + headers = record.response_headers or {} + record.response_content_type = headers.get("content-type", "") + record.response_content_length = headers.get("content-length", 0) + + @api.depends("request_body") + def _compute_request_preview(self): + for record in self.with_context(bin_size=False): + record.request_preview = record._body_preview(record.request_body) + + @api.depends("response_body") + def _compute_response_preview(self): + for record in self.with_context(bin_size=False): + record.response_preview = record._body_preview(record.response_body) + + def _body_preview(self, body): + # Display the first 1000 characters of the body if it's a text content + body_preview = False + if body: + try: + body_preview = body.decode("utf-8", errors="ignore") + if len(body_preview) > 1000: + body_preview = body_preview[:1000] + "...\n(...)" + except UnicodeDecodeError: + body_preview = False + return body_preview + + @api.depends("request_headers", "response_headers") + def _compute_headers_preview(self): + for record in self: + record.request_headers_preview = record._headers_preview( + record.request_headers + ) + record.response_headers_preview = record._headers_preview( + record.response_headers + ) + + def _headers_preview(self, headers): + return json.dumps(headers, sort_keys=True, indent=4) if headers else False + + @api.depends("request_body") + def _compute_request_b64(self): + self.request_b64 = base64.b64encode(self.request_body or b"") + + @api.depends("response_body") + def _compute_response_b64(self): + self.response_b64 = base64.b64encode(self.response_body or b"") diff --git a/fastapi_log/readme/CONTRIBUTORS.md b/fastapi_log/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..328a37da8 --- /dev/null +++ b/fastapi_log/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +- Florian Mounier diff --git a/fastapi_log/readme/DESCRIPTION.md b/fastapi_log/readme/DESCRIPTION.md new file mode 100644 index 000000000..60edac6e4 --- /dev/null +++ b/fastapi_log/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +This module allows an endpoint to activate full request logging in a database model. + +It is useful to debug production issues or to monitor the usage of a specific endpoint. diff --git a/fastapi_log/readme/USAGE.md b/fastapi_log/readme/USAGE.md new file mode 100644 index 000000000..420859a01 --- /dev/null +++ b/fastapi_log/readme/USAGE.md @@ -0,0 +1,6 @@ +To activate logging for an endpoint, you have to check the `Log Requests` checkbox in +the endpoint's configuration. This will log all requests and responses for that +endpoint. + +A smart button will be displayed in the endpoint's form view to access the endpoint +logs. A global log view is also available in the `FastAPI Logs` menu. diff --git a/fastapi_log/security/ir_model_access.xml b/fastapi_log/security/ir_model_access.xml new file mode 100644 index 000000000..ea4cd5edf --- /dev/null +++ b/fastapi_log/security/ir_model_access.xml @@ -0,0 +1,17 @@ + + + + + Fastapi Log: Read access + + + + + + + + diff --git a/fastapi_log/security/res_groups.xml b/fastapi_log/security/res_groups.xml new file mode 100644 index 000000000..3eec366d1 --- /dev/null +++ b/fastapi_log/security/res_groups.xml @@ -0,0 +1,17 @@ + + + + + + Fastapi Log Access + + + + diff --git a/fastapi_log/static/description/index.html b/fastapi_log/static/description/index.html new file mode 100644 index 000000000..0a76e9f5d --- /dev/null +++ b/fastapi_log/static/description/index.html @@ -0,0 +1,438 @@ + + + + + +Fastapi Log + + + +
+

Fastapi Log

+ + +

Beta License: AGPL-3 OCA/rest-framework Translate me on Weblate Try me on Runboat

+

This module allows an endpoint to activate full request logging in a +database model.

+

It is useful to debug production issues or to monitor the usage of a +specific endpoint.

+

Table of contents

+ +
+

Usage

+

To activate logging for an endpoint, you have to check the +Log Requests checkbox in the endpoint’s configuration. This will log +all requests and responses for that endpoint.

+

A smart button will be displayed in the endpoint’s form view to access +the endpoint logs. A global log view is also available in the +FastAPI Logs menu.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Akretion
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

paradoxxxzero

+

This module is part of the OCA/rest-framework project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/fastapi_log/tests/__init__.py b/fastapi_log/tests/__init__.py new file mode 100644 index 000000000..41a525a04 --- /dev/null +++ b/fastapi_log/tests/__init__.py @@ -0,0 +1 @@ +from . import test_fastapi_log diff --git a/fastapi_log/tests/test_fastapi_log.py b/fastapi_log/tests/test_fastapi_log.py new file mode 100644 index 000000000..77df06ee9 --- /dev/null +++ b/fastapi_log/tests/test_fastapi_log.py @@ -0,0 +1,161 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import os +import threading +import unittest +from contextlib import contextmanager + +from odoo.sql_db import TestCursor +from odoo.tests.common import HttpCase, RecordCapturer + +from odoo.addons.fastapi.schemas import DemoExceptionType + +from fastapi import status + + +@unittest.skipIf(os.getenv("SKIP_HTTP_CASE"), "FastAPIEncryptedErrorsCase skipped") +class FastAPIEncryptedErrorsCase(HttpCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.fastapi_demo_app = cls.env.ref("fastapi.fastapi_endpoint_demo") + cls.fastapi_demo_app._handle_registry_sync() + cls.fastapi_demo_app.write({"log_requests": True}) + lang = ( + cls.env["res.lang"] + .with_context(active_test=False) + .search([("code", "=", "fr_BE")]) + ) + lang.active = True + + def setUp(self): + super().setUp() + # Use a side test cursor to be able to get exception logs + reg = self.env.registry + reg.test_log_lock = threading.RLock() + reg.test_log_cr = TestCursor(reg._db.cursor(), reg.test_log_lock) + + def tearDown(self): + reg = self.env.registry + reg.test_log_cr.rollback() + reg.test_log_cr.close() + reg.test_log_cr = None + reg.test_log_lock = None + super().tearDown() + + @contextmanager + def log_capturer(self): + with RecordCapturer( + self.env(cr=self.env.registry.test_log_cr)["fastapi.log"], + [("endpoint_id", "=", self.fastapi_demo_app.id)], + ) as capturer: + yield capturer + + def test_no_log_if_disabled(self): + self.fastapi_demo_app.write({"log_requests": False}) + + with self.log_capturer() as capturer: + response = self.url_open("/fastapi_demo/demo", timeout=200) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + self.assertFalse(capturer.records) + + def test_log_simple(self): + with self.log_capturer() as capturer: + response = self.url_open("/fastapi_demo/demo", timeout=200) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + self.assertEqual(len(capturer.records), 1) + log = capturer.records[0] + self.assertTrue(log.request_url.endswith("/fastapi_demo/demo")) + self.assertEqual(log.request_method, "GET") + self.assertEqual(log.response_status_code, 200) + self.assertTrue(log.time > 0) + + def test_log_exception(self): + with self.log_capturer() as capturer: + route = ( + "/fastapi_demo/demo/exception?" + f"exception_type={DemoExceptionType.user_error.value}" + "&error_message=User Error" + ) + response = self.url_open(route, timeout=200) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + self.assertEqual(len(capturer.records), 1) + log = capturer.records[0] + self.assertIn("/fastapi_demo/demo/exception", log.request_url) + self.assertEqual(log.request_method, "GET") + self.assertEqual(log.response_status_code, 400) + self.assertTrue(log.time > 0) + self.assertTrue(log.response_body) + self.assertIn(b"User Error", log.response_body) + self.assertIn("odoo.exceptions.UserError: User Error\n", log.stack_trace) + + def test_log_bare_exception(self): + with self.log_capturer() as capturer: + route = ( + "/fastapi_demo/demo/exception?" + f"exception_type={DemoExceptionType.bare_exception.value}" + "&error_message=Internal Server Error" + ) + response = self.url_open(route, timeout=200) + self.assertEqual( + response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR + ) + + self.assertEqual(len(capturer.records), 1) + log = capturer.records[0] + self.assertIn("/fastapi_demo/demo/exception", log.request_url) + self.assertEqual(log.request_method, "GET") + self.assertEqual(log.response_status_code, 500) + self.assertTrue(log.time > 0) + self.assertTrue(log.response_body) + self.assertIn(b"Internal Server Error", log.response_body) + self.assertIn("NotImplementedError: Internal Server Error\n", log.stack_trace) + + def test_log_retrying_post(self): + with self.log_capturer() as capturer: + nbr_retries = 2 + route = f"/fastapi_demo/demo/retrying?nbr_retries={nbr_retries}" + response = self.url_open( + route, timeout=20, files={"file": ("test.txt", b"test")} + ) + self.assertEqual(response.status_code, 200) + self.assertDictEqual( + response.json(), {"retries": nbr_retries, "file": "test"} + ) + + self.assertEqual(len(capturer.records), 3) + log = capturer.records[0] + self.assertIn("/fastapi_demo/demo/retrying", log.request_url) + self.assertEqual(log.request_method, "POST") + self.assertEqual(log.response_status_code, 200) + self.assertTrue(log.time > 0) + self.assertTrue(log.response_body) + self.assertIn(b'"retries":2', log.response_body) + self.assertIn(b'"file":"test"', log.response_body) + self.assertFalse(log.stack_trace) + log = capturer.records[1] + self.assertIn("/fastapi_demo/demo/retrying", log.request_url) + self.assertEqual(log.request_method, "POST") + self.assertEqual(log.response_status_code, 500) + self.assertTrue(log.time > 0) + self.assertTrue(log.response_body) + self.assertIn(b'{"detail": "Internal Server Error"}', log.response_body) + self.assertIn( + "odoo.addons.fastapi.routers.demo_router.FakeConcurrentUpdateError: fake error", + log.stack_trace, + ) + log = capturer.records[2] + self.assertIn("/fastapi_demo/demo/retrying", log.request_url) + self.assertEqual(log.request_method, "POST") + self.assertEqual(log.response_status_code, 500) + self.assertTrue(log.time > 0) + self.assertTrue(log.response_body) + self.assertIn(b'{"detail": "Internal Server Error"}', log.response_body) + self.assertIn( + "odoo.addons.fastapi.routers.demo_router.FakeConcurrentUpdateError: fake error", + log.stack_trace, + ) diff --git a/fastapi_log/views/fastapi_endpoint_views.xml b/fastapi_log/views/fastapi_endpoint_views.xml new file mode 100644 index 000000000..7997cd9c0 --- /dev/null +++ b/fastapi_log/views/fastapi_endpoint_views.xml @@ -0,0 +1,42 @@ + + + + + + Fastapi Log + fastapi.log + tree,form + [('endpoint_id', '=', active_id)] + {'default_endpoint_id': active_id} + + + + + fastapi.endpoint + + + +
+ +
+ + + + + + +
+ +
diff --git a/fastapi_log/views/fastapi_log_views.xml b/fastapi_log/views/fastapi_log_views.xml new file mode 100644 index 000000000..021442ffc --- /dev/null +++ b/fastapi_log/views/fastapi_log_views.xml @@ -0,0 +1,124 @@ + + + + + Fastapi Log + fastapi.log + tree,form + + + + fastapi.log.form + fastapi.log + +
+ +
+

+ +

+
+ + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ + + fastapi.log.tree + fastapi.log + + + + + + + + + + + + + + + + fastapi.log.search + fastapi.log + + + + + + + + + + + + + + + + + + +
diff --git a/setup/fastapi_log/odoo/addons/fastapi_log b/setup/fastapi_log/odoo/addons/fastapi_log new file mode 120000 index 000000000..4996c1e31 --- /dev/null +++ b/setup/fastapi_log/odoo/addons/fastapi_log @@ -0,0 +1 @@ +../../../../fastapi_log \ No newline at end of file diff --git a/setup/fastapi_log/setup.py b/setup/fastapi_log/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/fastapi_log/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)