diff --git a/api_log/README.rst b/api_log/README.rst new file mode 100644 index 000000000..9cc98c41c --- /dev/null +++ b/api_log/README.rst @@ -0,0 +1,93 @@ +======= +API 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/api_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-api_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 to store request and response logs for any API. + +When a response is logged, the header ``API_LOG_ENTRY_ID`` is injected +in the response header. This header stores the identifier of the log +record produced from the response. + +**Table of contents** + +.. contents:: + :local: + +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 +- Guewen Baconnier guewen.baconnier@camptocamp.com +- Simone Orsi simahawk@gmail.com +- `PyTech `__: + + - Simone Rubino + +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/api_log/__init__.py b/api_log/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/api_log/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/api_log/__manifest__.py b/api_log/__manifest__.py new file mode 100644 index 000000000..84193a908 --- /dev/null +++ b/api_log/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "API Log", + "version": "16.0.1.0.0", + "author": "Akretion, Odoo Community Association (OCA)", + "license": "AGPL-3", + "summary": "Log API requests in database", + "category": "Tools", + "depends": ["web"], + "website": "https://github.com/OCA/rest-framework", + "data": [ + "security/res_groups.xml", + "security/ir_model_access.xml", + "views/api_log_views.xml", + ], + "maintainers": ["paradoxxxzero"], +} diff --git a/api_log/models/__init__.py b/api_log/models/__init__.py new file mode 100644 index 000000000..2f4388e55 --- /dev/null +++ b/api_log/models/__init__.py @@ -0,0 +1,2 @@ +from . import api_log_collection +from . import api_log diff --git a/api_log/models/api_log.py b/api_log/models/api_log.py new file mode 100644 index 000000000..106ffa330 --- /dev/null +++ b/api_log/models/api_log.py @@ -0,0 +1,275 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import base64 +import json +import time +from traceback import format_exception + +from werkzeug.exceptions import HTTPException as WerkzeugHTTPException + +from odoo import api, fields, models + + +class APILog(models.Model): + _name = "api.log" + _description = "Log for API" + _order = "id desc" + + collection_ref = fields.Reference( + selection="_selection_collection_ref", + ) + collection_model = fields.Char( + compute="_compute_collection", + store=True, + index=True, + ) + collection_id = fields.Integer( + compute="_compute_collection", + store=True, + 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") + # Binary fields are useful to download the payload in case of file download/upload + 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 + ) + + @api.model + def _selection_collection_ref(self): + return [] + + @api.depends( + "collection_ref", + ) + def _compute_collection(self): + for log in self: + collection = log.collection_ref + if collection: + collection_model = collection._name + collection_id = collection.id + else: + collection_model = False + collection_id = False + log.collection_model = collection_model + log.collection_id = collection_id + + @api.model + def _headers_hidden_keys(self): + """Header keys that should not be logged. + + They might contains sensitive data. + """ + return ( + "Api-Key", + "Cookie", + ) + + @api.model + def _sanitize_headers_dict(self, headers_dict): + keys_to_hide = self._headers_hidden_keys() + for key in headers_dict: + if key in keys_to_hide: + headers_dict[key] = "" + return headers_dict + + @api.model + def _headers_to_dict(self, headers): + headers_dict = {key: value for key, value in headers.items()} + return self._sanitize_headers_dict(headers_dict) + + def _current_time(self): + return time.time_ns() / 1e9 + + @api.model + def _get_http_request(self, request): + return request.httprequest + + @api.model + def _get_request_body(self, request): + """Take extra care with the request's body because it might get consumed.""" + httprequest = self._get_http_request(request) + return httprequest.data + + @api.model + def _prepare_log_request(self, request): + httprequest = self._get_http_request(request) + log_request_values = { + "request_url": httprequest.url, + "request_method": httprequest.method, + "request_headers": self._headers_to_dict(httprequest.headers), + "request_body": self._get_request_body(request), + "request_date": fields.Datetime.now(), + "request_time": self._current_time(), + } + return log_request_values + + @api.model + def log_request(self, request, override_log_values=None): + log_request_values = self._prepare_log_request(request) + log_request_values.update(override_log_values or {}) + return self.sudo().create(log_request_values) + + def _inject_log_entry(self, values_dict): + values_dict["API-Log-Entry-ID"] = str(self.id) + return values_dict + + def _prepare_log_response(self, response): + self._inject_log_entry(response.headers) + headers_dict = self._headers_to_dict(response.headers) + return { + "response_status_code": response.status_code, + "response_headers": headers_dict, + "response_body": response.data, + "response_date": fields.Datetime.now(), + "response_time": self._current_time(), + } + + def log_response(self, response): + log_response_values = self._prepare_log_response(response) + return self.sudo().write(log_response_values) + + def _prepare_log_exception(self, exception): + exception.headers = getattr(exception, "headers", {}) + values = { + "stack_trace": "".join(format_exception(exception)), + "response_headers": self._inject_log_entry(exception.headers), + "response_body": str(exception), + "response_date": fields.Datetime.now(), + "response_time": self._current_time(), + } + + if isinstance(exception, WerkzeugHTTPException): + values.update( + { + "response_status_code": exception.code, + "response_headers": self._headers_to_dict(exception.get_headers()), + "response_body": exception.get_body(), + } + ) + return values + + def log_exception(self, exception): + try: + exc_handling_response = self.env.registry["ir.http"]._handle_error( + exception + ) + self.log_response(exc_handling_response) + except Exception as handling_exception: + exception = handling_exception + log_exception_values = self._prepare_log_exception(exception) + return self.sudo().write(log_exception_values) + + @api.depends("request_url", "request_method", "request_date") + def _compute_name(self): + for log in self: + log.name = ( + f"{log.request_date.isoformat()} - " + f"[{log.request_method}] {log.request_url}" + ) + + @api.depends("request_time", "response_time") + def _compute_time(self): + for log in self: + if log.request_time and log.response_time: + log.time = log.response_time - log.request_time + else: + log.time = 0 + + @api.depends("request_headers") + def _compute_request_headers_derived(self): + for log in self: + headers = log.request_headers or {} + log.request_content_type = headers.get("content-type", "") + log.request_content_length = headers.get("content-length", 0) + log.referrer = headers.get("referer", "") + + @api.depends("response_headers") + def _compute_response_headers_derived(self): + for log in self: + headers = log.response_headers or {} + log.response_content_type = headers.get("content-type", "") + log.response_content_length = headers.get("content-length", 0) + + @api.depends("request_body") + def _compute_request_preview(self): + for log in self.with_context(bin_size=False): + log.request_preview = log._body_preview(log.request_body) + + @api.depends("response_body") + def _compute_response_preview(self): + for log in self.with_context(bin_size=False): + log.response_preview = log._body_preview(log.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 log in self: + log.request_headers_preview = log._headers_preview(log.request_headers) + log.response_headers_preview = log._headers_preview(log.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): + for log in self: + log.request_b64 = base64.b64encode(log.request_body or b"") + + @api.depends("response_body") + def _compute_response_b64(self): + for log in self: + log.response_b64 = base64.b64encode(log.response_body or b"") diff --git a/api_log/models/api_log_collection.py b/api_log/models/api_log_collection.py new file mode 100644 index 000000000..a094b6e0e --- /dev/null +++ b/api_log/models/api_log_collection.py @@ -0,0 +1,55 @@ +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class APILogCollection(models.AbstractModel): + _name = "api.log_collection.mixin" + _description = "Collection of API logs" + + log_requests = fields.Boolean( + help="Log requests in database.", + ) + + log_ids = fields.One2many( + comodel_name="api.log", + compute="_compute_log_ids", + string="Logs", + ) + + def _get_logs_domain(self): + """Domain to find the logs in `self`.""" + return [ + ("collection_model", "=", self._name), + ("collection_id", "in", self.ids), + ] + + def _compute_log_ids(self): + all_logs = self.env["api.log"].search_read( + domain=self._get_logs_domain(), + fields=[ + "collection_id", + ], + load=None, + ) + log_ids_by_collection_id = {} + for log in all_logs: + log_ids_by_collection_id.setdefault(log["collection_id"], []).append( + log["id"] + ) + + for collection in self: + collection.log_ids = log_ids_by_collection_id.get(collection.id) + + def action_logs(self): + return { + "type": "ir.actions.act_window", + "res_model": "api.log", + "name": "Logs", + "view_type": "form", + "view_mode": "tree,form", + "target": "current", + "domain": self._get_logs_domain(), + "context": dict(self.env.context), + } diff --git a/api_log/readme/CONTRIBUTORS.md b/api_log/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..599c28bb2 --- /dev/null +++ b/api_log/readme/CONTRIBUTORS.md @@ -0,0 +1,5 @@ +- Florian Mounier +- Guewen Baconnier +- Simone Orsi +- [PyTech](https://www.pytech.it): + - Simone Rubino \<\> diff --git a/api_log/readme/DESCRIPTION.md b/api_log/readme/DESCRIPTION.md new file mode 100644 index 000000000..e620776ad --- /dev/null +++ b/api_log/readme/DESCRIPTION.md @@ -0,0 +1,4 @@ +This module allows to store request and response logs for any API. + +When a response is logged, the header `API_LOG_ENTRY_ID` is injected in the response header. +This header stores the identifier of the log record produced from the response. diff --git a/api_log/security/ir_model_access.xml b/api_log/security/ir_model_access.xml new file mode 100644 index 000000000..a092c0d3a --- /dev/null +++ b/api_log/security/ir_model_access.xml @@ -0,0 +1,17 @@ + + + + + API Log: Read access + + + + + + + + diff --git a/api_log/security/res_groups.xml b/api_log/security/res_groups.xml new file mode 100644 index 000000000..8b9ddf38b --- /dev/null +++ b/api_log/security/res_groups.xml @@ -0,0 +1,17 @@ + + + + + + API Log Access + + + + diff --git a/api_log/static/description/index.html b/api_log/static/description/index.html new file mode 100644 index 000000000..60378ff49 --- /dev/null +++ b/api_log/static/description/index.html @@ -0,0 +1,434 @@ + + + + + +API Log + + + +
+

API Log

+ + +

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

+

This module allows to store request and response logs for any API.

+

When a response is logged, the header API_LOG_ENTRY_ID is injected +in the response header. This header stores the identifier of the log +record produced from the response.

+

Table of contents

+ +
+

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

+ +
+
+

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/api_log/tests/__init__.py b/api_log/tests/__init__.py new file mode 100644 index 000000000..7f84a8e4f --- /dev/null +++ b/api_log/tests/__init__.py @@ -0,0 +1 @@ +from . import test_api_log diff --git a/api_log/tests/common.py b/api_log/tests/common.py new file mode 100644 index 000000000..e02138286 --- /dev/null +++ b/api_log/tests/common.py @@ -0,0 +1,11 @@ +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests.common import HttpCase + + +class Common(HttpCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.log_model = cls.env["api.log"] diff --git a/api_log/tests/test_api_log.py b/api_log/tests/test_api_log.py new file mode 100644 index 000000000..45ddd5958 --- /dev/null +++ b/api_log/tests/test_api_log.py @@ -0,0 +1,51 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import requests + +from odoo.http import Request, Response + +from odoo.addons.api_log.tests.common import Common + + +class TestAPILog(Common): + def test_log_request(self): + base_url = self.base_url() + secret_api_key = "my-secret-api-key" + secret_cookie = "my-secret-biscuit" + public_header_value = "public_header_value" + httprequest = requests.Request( + headers={ + "Api-Key": secret_api_key, + "Cookie": secret_cookie, + "Public-Header": public_header_value, + }, + url=base_url, + method="GET", + ) + request = Request(httprequest) + log = self.log_model.log_request(request) + + self.assertEqual(log.request_url, base_url) + self.assertEqual(log.request_method, "GET") + headers_values = log.request_headers.values() + self.assertNotIn(secret_api_key, headers_values) + self.assertNotIn(secret_cookie, headers_values) + self.assertIn(public_header_value, headers_values) + + def test_log_response(self): + response = Response() + log = self.log_model.create({}) + log.log_response(response) + + self.assertEqual(log.response_status_code, 200) + self.assertEqual(log.response_headers["API-Log-Entry-ID"], str(log.id)) + self.assertEqual(response.headers["API-Log-Entry-ID"], str(log.id)) + + def test_log_exception(self): + log = self.log_model.create({}) + log.log_exception(Exception()) + + self.assertEqual(log.response_headers["API-Log-Entry-ID"], str(log.id)) diff --git a/api_log/views/api_log_views.xml b/api_log/views/api_log_views.xml new file mode 100644 index 000000000..4e8ba689f --- /dev/null +++ b/api_log/views/api_log_views.xml @@ -0,0 +1,118 @@ + + + + + API Log + api.log + tree,form + + + + api.log.form + api.log + +
+ +
+

+ +

+
+ + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ + + api.log.tree + api.log + + + + + + + + + + + + + + + + api.log.search + api.log + + + + + + + + + + + + + + + + + + +
diff --git a/api_log_mail/README.rst b/api_log_mail/README.rst new file mode 100644 index 000000000..57e228500 --- /dev/null +++ b/api_log_mail/README.rst @@ -0,0 +1,93 @@ +==================== +API Log notification +==================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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/api_log_mail + :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-api_log_mail + :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 to create an activity when an exception is logged in +an API logs collection. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +In any log collection that has logging enabled, insert an activity type +in "Error Activity type". + +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 +------- + +* PyTech + +Contributors +------------ + +- `PyTech `__: + + - Simone Rubino + +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-SirPyTech| image:: https://github.com/SirPyTech.png?size=40px + :target: https://github.com/SirPyTech + :alt: SirPyTech + +Current `maintainer `__: + +|maintainer-SirPyTech| + +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/api_log_mail/__init__.py b/api_log_mail/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/api_log_mail/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/api_log_mail/__manifest__.py b/api_log_mail/__manifest__.py new file mode 100644 index 000000000..d334b7f9c --- /dev/null +++ b/api_log_mail/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "API Log notification", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "PyTech, Odoo Community Association (OCA)", + "maintainers": [ + "SirPyTech", + ], + "website": "https://github.com/OCA/rest-framework", + "summary": "Notify logged exceptions.", + "category": "Tools", + "depends": [ + "api_log", + "mail", + ], +} diff --git a/api_log_mail/models/__init__.py b/api_log_mail/models/__init__.py new file mode 100644 index 000000000..13ae7379a --- /dev/null +++ b/api_log_mail/models/__init__.py @@ -0,0 +1,4 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import api_log_collection +from . import api_log diff --git a/api_log_mail/models/api_log.py b/api_log_mail/models/api_log.py new file mode 100644 index 000000000..36ca3ec90 --- /dev/null +++ b/api_log_mail/models/api_log.py @@ -0,0 +1,41 @@ +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, models + + +class APILog(models.Model): + _name = "api.log" + _inherit = [ + "api.log", + "mail.activity.mixin", + # mail.thread is needed + # because message_subscribe is called + # during activity creation + "mail.thread", + ] + _mail_post_access = "read" # Access required to open an activity + + @api.model + def log_request(self, request, override_log_values=None): + return super( + APILog, + self.with_context(tracking_disable=True), + ).log_request(request, override_log_values=override_log_values) + + def _notify_api_log_exception(self): + if collection := self.collection_ref: + activity_type = collection.api_log_mail_exception_activity_type_id + if activity_type: + self.sudo().activity_schedule( + activity_type_id=activity_type.id, + ) + + mail_template = collection.api_log_mail_exception_template_id + if mail_template: + mail_template.sudo().send_mail(self.id) + + def log_exception(self, exception): + res = super().log_exception(exception) + self._notify_api_log_exception() + return res diff --git a/api_log_mail/models/api_log_collection.py b/api_log_mail/models/api_log_collection.py new file mode 100644 index 000000000..cf7a87336 --- /dev/null +++ b/api_log_mail/models/api_log_collection.py @@ -0,0 +1,21 @@ +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class APILogCollection(models.AbstractModel): + _inherit = "api.log_collection.mixin" + + api_log_mail_exception_template_id = fields.Many2one( + comodel_name="mail.template", + domain=[("model_id.model", "=", "api.log")], + string="Error E-mail Template", + help="An email based on this template will be sent when an error is logged.", + ) + api_log_mail_exception_activity_type_id = fields.Many2one( + comodel_name="mail.activity.type", + domain=[("res_model", "=", "api.log")], + string="Error Activity type", + help="An activity of this type will be created when an error is logged.", + ) diff --git a/api_log_mail/readme/CONFIGURE.md b/api_log_mail/readme/CONFIGURE.md new file mode 100644 index 000000000..8c16db49d --- /dev/null +++ b/api_log_mail/readme/CONFIGURE.md @@ -0,0 +1 @@ +In any log collection that has logging enabled, insert an activity type in "Error Activity type". diff --git a/api_log_mail/readme/CONTRIBUTORS.md b/api_log_mail/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..6e720b67d --- /dev/null +++ b/api_log_mail/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- [PyTech](https://www.pytech.it): + - Simone Rubino \<\> diff --git a/api_log_mail/readme/DESCRIPTION.md b/api_log_mail/readme/DESCRIPTION.md new file mode 100644 index 000000000..f1207db61 --- /dev/null +++ b/api_log_mail/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module allows to create an activity when an exception is logged in an API logs collection. diff --git a/api_log_mail/static/description/index.html b/api_log_mail/static/description/index.html new file mode 100644 index 000000000..2d5a03422 --- /dev/null +++ b/api_log_mail/static/description/index.html @@ -0,0 +1,435 @@ + + + + + +API Log notification + + + +
+

API Log notification

+ + +

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

+

This module allows to create an activity when an exception is logged in +an API logs collection.

+

Table of contents

+ +
+

Configuration

+

In any log collection that has logging enabled, insert an activity type +in “Error Activity type”.

+
+
+

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

+
    +
  • PyTech
  • +
+
+
+

Contributors

+ +
+
+

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:

+

SirPyTech

+

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/README.rst b/fastapi_log/README.rst new file mode 100644 index 000000000..fdb54e937 --- /dev/null +++ b/fastapi_log/README.rst @@ -0,0 +1,102 @@ +=========== +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 +- `PyTech `__: + + - Simone Rubino + +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..27938d00e --- /dev/null +++ b/fastapi_log/__manifest__.py @@ -0,0 +1,24 @@ +# 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.1.0", + "author": "Akretion, Odoo Community Association (OCA)", + "summary": "Log Fastapi requests in database", + "category": "Tools", + "depends": [ + "api_log", + "fastapi", + ], + "website": "https://github.com/OCA/rest-framework", + "data": [ + "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..a6978bc57 --- /dev/null +++ b/fastapi_log/fastapi_dispatcher.py @@ -0,0 +1,79 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import logging +from contextlib import contextmanager + +from odoo import registry +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" + + @contextmanager + def _create_log_env(self, request_env): + request_registry = request_env.registry + if request_registry.in_test_mode(): + # During tests, use the dedicated test's cursor + cr = request_registry.test_log_cr + else: + # Create an independent cursor + # so the logs are committed despite any endpoint's exceptions + cr = registry(request_registry.db_name).cursor() + + try: + yield request_env(cr=cr, su=True) + finally: + # While executing tests, + # the cursor is already managed in the tests + if not request_registry.in_test_mode(): + try: + cr.commit() # pylint: disable=invalid-commit + finally: + cr.close() + + def dispatch(self, endpoint, args): + self.request.params = {} + environ = self._get_environ() + fastapi_endpoint = ( + self.request.env["fastapi.endpoint"] + .sudo() + ._get_endpoint(environ["PATH_INFO"]) + ) + if fastapi_endpoint.log_requests: + with self._create_log_env(self.request.env) as log_env: + try: + log = log_env["api.log"].log_request(self.request) + except Exception as e: + _logger.warning("Failed to log request", exc_info=e) + log = None + + try: + response = super().dispatch(endpoint, args) + except Exception as response_exc: + try: + log and log.log_exception(response_exc) + except Exception as e: + _logger.warning("Failed to log exception", exc_info=e) + + raise response_exc + else: + try: + log and log.log_response(response) + except Exception as e: + _logger.warning("Failed to log response", exc_info=e) + + return response + else: + return super().dispatch(endpoint, args) diff --git a/fastapi_log/migrations/16.0.1.1.0/post-migration.py b/fastapi_log/migrations/16.0.1.1.0/post-migration.py new file mode 100644 index 000000000..b95831de3 --- /dev/null +++ b/fastapi_log/migrations/16.0.1.1.0/post-migration.py @@ -0,0 +1,32 @@ +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from openupgradelib import openupgrade + + +@openupgrade.migrate() +def migrate(env, version): + endpoint_id_column = openupgrade.get_legacy_name("fastapi_endpoint_id") + openupgrade.logged_query( + env.cr, + """ + UPDATE api_log SET + collection_id=%(endpoint_id_column)s, + collection_model='fastapi.endpoint', + collection_ref='fastapi.endpoint,' || %(endpoint_id_column)s + WHERE %(endpoint_id_column)s IS NOT NULL + """ + % { + "endpoint_id_column": endpoint_id_column, + }, + ) + openupgrade.logged_query( + env.cr, + """ + ALTER TABLE api_log + DROP COLUMN %(endpoint_id_column)s + """ + % { + "endpoint_id_column": endpoint_id_column, + }, + ) diff --git a/fastapi_log/migrations/16.0.1.1.0/pre-migration.py b/fastapi_log/migrations/16.0.1.1.0/pre-migration.py new file mode 100644 index 000000000..9cdaa9143 --- /dev/null +++ b/fastapi_log/migrations/16.0.1.1.0/pre-migration.py @@ -0,0 +1,20 @@ +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from openupgradelib import openupgrade + + +@openupgrade.migrate() +def migrate(env, version): + openupgrade.copy_columns( + env.cr, + { + "api_log": [ + ( + "fastapi_endpoint_id", + None, + None, + ), + ], + }, + ) diff --git a/fastapi_log/models/__init__.py b/fastapi_log/models/__init__.py new file mode 100644 index 000000000..23ac9cf0b --- /dev/null +++ b/fastapi_log/models/__init__.py @@ -0,0 +1,2 @@ +from . import api_log +from . import fastapi_endpoint diff --git a/fastapi_log/models/api_log.py b/fastapi_log/models/api_log.py new file mode 100644 index 000000000..086e656b4 --- /dev/null +++ b/fastapi_log/models/api_log.py @@ -0,0 +1,65 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from starlette.exceptions import HTTPException as StarletteHTTPException + +from odoo import api, models + + +class FastapiLog(models.Model): + _inherit = "api.log" + + @api.model + def _selection_collection_ref(self): + collections = super()._selection_collection_ref() + fastapi_endpoint_model = self.env["fastapi.endpoint"] + collections.append( + (fastapi_endpoint_model._name, fastapi_endpoint_model._description) + ) + return collections + + @api.model + def _get_request_body(self, request): + # Be careful to not consume the request body if it hasn't been wrapped + dispatcher = request.dispatcher + if dispatcher.routing_type == "fastapi": + environ = dispatcher._get_environ() + stream = environ.get("wsgi.input") + if stream and stream.seekable(): + request_body = stream.read() + stream.seek(0) + else: + request_body = super()._get_request_body(request) + return request_body + + @api.model + def _prepare_log_request(self, request): + log_request_values = super()._prepare_log_request(request) + dispatcher = request.dispatcher + if dispatcher.routing_type == "fastapi": + environ = dispatcher._get_environ() + endpoint = ( + request.env["fastapi.endpoint"] + .sudo() + ._get_endpoint(environ["PATH_INFO"]) + ) + log_request_values["collection_ref"] = "%s,%s" % ( + endpoint._name, + endpoint.id, + ) + + return log_request_values + + def _prepare_log_exception(self, exception): + values = super()._prepare_log_exception(exception) + if isinstance(exception, StarletteHTTPException): + values.update( + { + "response_status_code": exception.status_code, + "response_headers": self._headers_to_dict(exception.headers), + "response_body": exception.detail, + } + ) + return values diff --git a/fastapi_log/models/fastapi_endpoint.py b/fastapi_log/models/fastapi_endpoint.py new file mode 100644 index 000000000..4770dcf24 --- /dev/null +++ b/fastapi_log/models/fastapi_endpoint.py @@ -0,0 +1,14 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models + + +class FastapiEndpoint(models.Model): + _name = "fastapi.endpoint" + _inherit = [ + "api.log_collection.mixin", + "fastapi.endpoint", + ] diff --git a/fastapi_log/readme/CONTRIBUTORS.md b/fastapi_log/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..1e935bfb5 --- /dev/null +++ b/fastapi_log/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +- Florian Mounier +- [PyTech](https://www.pytech.it): + - Simone Rubino \<\> 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/static/description/index.html b/fastapi_log/static/description/index.html new file mode 100644 index 000000000..b0b206a30 --- /dev/null +++ b/fastapi_log/static/description/index.html @@ -0,0 +1,442 @@ + + + + + +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
  • +
+
+
+

Contributors

+ +
+
+

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/common.py b/fastapi_log/tests/common.py new file mode 100644 index 000000000..013dba216 --- /dev/null +++ b/fastapi_log/tests/common.py @@ -0,0 +1,60 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import threading +from contextlib import contextmanager + +from odoo.sql_db import TestCursor +from odoo.tests.common import RecordCapturer + +from odoo.addons.api_log.tests.common import Common as CommonAPILog + + +class Common(CommonAPILog): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.fastapi_demo_app = cls.env.ref("fastapi.fastapi_endpoint_demo") + cls.fastapi_demo_app.root_path += "/test" + 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() + + def _get_log_env(self): + return self.env(cr=self.env.registry.test_log_cr) + + def _get_log_env_records(self, records): + log_env = self._get_log_env() + return log_env[records._name].browse(records.ids) + + @contextmanager + def log_capturer(self): + app = self.fastapi_demo_app + log_env = self._get_log_env() + with RecordCapturer( + log_env[self.log_model._name], + [("collection_ref", "=", "%s,%s" % (app._name, app.id))], + ) as capturer: + yield capturer diff --git a/fastapi_log/tests/test_fastapi_log.py b/fastapi_log/tests/test_fastapi_log.py new file mode 100644 index 000000000..c5d7bf169 --- /dev/null +++ b/fastapi_log/tests/test_fastapi_log.py @@ -0,0 +1,131 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import os +import unittest + +from odoo.addons.fastapi.schemas import DemoExceptionType +from odoo.addons.fastapi_log.tests.common import Common + +from fastapi import status + + +@unittest.skipIf(os.getenv("SKIP_HTTP_CASE"), "TestFastapiLog skipped") +class TestFastapiLog(Common): + 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/test/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/test/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/test/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/test/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.assertEqual(response.headers["API-Log-Entry-ID"], str(log.id)) + + self.assertIn("/fastapi_demo/test/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/test/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/test/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/test/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) + for log in capturer.records[1:]: + self.assertIn("/fastapi_demo/test/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"fake error", log.response_body) + self.assertIn( + "odoo.addons.fastapi.routers.demo_router.FakeConcurrentUpdateError: fake error", + log.stack_trace, + ) + + log = capturer.records[0] + self.assertIn("/fastapi_demo/test/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) + + def test_collection_ref(self): + """The created log holds a reference to its endpoint and viceversa.""" + # Arrange + endpoint = self.fastapi_demo_app + # pre-condition + self.assertFalse(endpoint.log_ids) + + # Act + with self.log_capturer() as capturer: + self.url_open("/fastapi_demo/test/demo", timeout=200) + + # Assert + log = capturer.records[-1] + self.assertEqual(log.collection_ref, endpoint) + self.assertIn(log, log.collection_ref.log_ids) diff --git a/fastapi_log/views/fastapi_endpoint_views.xml b/fastapi_log/views/fastapi_endpoint_views.xml new file mode 100644 index 000000000..ac50560e8 --- /dev/null +++ b/fastapi_log/views/fastapi_endpoint_views.xml @@ -0,0 +1,41 @@ + + + + + + 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..3c19ba737 --- /dev/null +++ b/fastapi_log/views/fastapi_log_views.xml @@ -0,0 +1,25 @@ + + + + + Fastapi Logs + api.log + [ + ("collection_model", "=", "fastapi.endpoint"), + ] + tree,form + + + + diff --git a/fastapi_log_mail/README.rst b/fastapi_log_mail/README.rst new file mode 100644 index 000000000..af99233ca --- /dev/null +++ b/fastapi_log_mail/README.rst @@ -0,0 +1,92 @@ +======================== +FastAPI Log notification +======================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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_mail + :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_mail + :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 to create an activity when an exception is logged in +a fastapi endpoint. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +Configure a fastapi endpoint as explained in ``api_log_mail``. + +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 +------- + +* PyTech + +Contributors +------------ + +- `PyTech `__: + + - Simone Rubino + +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-SirPyTech| image:: https://github.com/SirPyTech.png?size=40px + :target: https://github.com/SirPyTech + :alt: SirPyTech + +Current `maintainer `__: + +|maintainer-SirPyTech| + +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_mail/__init__.py b/fastapi_log_mail/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/fastapi_log_mail/__manifest__.py b/fastapi_log_mail/__manifest__.py new file mode 100644 index 000000000..8bcbc0621 --- /dev/null +++ b/fastapi_log_mail/__manifest__.py @@ -0,0 +1,22 @@ +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "FastAPI Log notification", + "version": "16.0.1.1.0", + "license": "AGPL-3", + "author": "PyTech, Odoo Community Association (OCA)", + "maintainers": [ + "SirPyTech", + ], + "website": "https://github.com/OCA/rest-framework", + "summary": "Notify logged exceptions.", + "category": "Tools", + "depends": [ + "fastapi_log", + "api_log_mail", + ], + "data": [ + "views/fastapi_endpoint_views.xml", + ], +} diff --git a/fastapi_log_mail/migrations/16.0.1.1.0/post-migration.py b/fastapi_log_mail/migrations/16.0.1.1.0/post-migration.py new file mode 100644 index 000000000..1903d6e19 --- /dev/null +++ b/fastapi_log_mail/migrations/16.0.1.1.0/post-migration.py @@ -0,0 +1,32 @@ +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from openupgradelib import openupgrade + + +@openupgrade.migrate() +def migrate(env, version): + template_id_column = openupgrade.get_legacy_name( + "fastapi_log_mail_template_id", + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE fastapi_endpoint SET + api_log_mail_exception_template_id=%(template_id_column)s + WHERE %(template_id_column)s IS NOT NULL + """ + % { + "template_id_column": template_id_column, + }, + ) + openupgrade.logged_query( + env.cr, + """ + ALTER TABLE fastapi_endpoint + DROP COLUMN %(template_id_column)s + """ + % { + "template_id_column": template_id_column, + }, + ) diff --git a/fastapi_log_mail/migrations/16.0.1.1.0/pre-migration.py b/fastapi_log_mail/migrations/16.0.1.1.0/pre-migration.py new file mode 100644 index 000000000..71f919a98 --- /dev/null +++ b/fastapi_log_mail/migrations/16.0.1.1.0/pre-migration.py @@ -0,0 +1,20 @@ +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from openupgradelib import openupgrade + + +@openupgrade.migrate() +def migrate(env, version): + openupgrade.copy_columns( + env.cr, + { + "fastapi_endpoint": [ + ( + "fastapi_log_mail_template_id", + None, + None, + ), + ], + }, + ) diff --git a/fastapi_log_mail/readme/CONFIGURE.md b/fastapi_log_mail/readme/CONFIGURE.md new file mode 100644 index 000000000..ca1622a8b --- /dev/null +++ b/fastapi_log_mail/readme/CONFIGURE.md @@ -0,0 +1 @@ +Configure a fastapi endpoint as explained in `api_log_mail`. diff --git a/fastapi_log_mail/readme/CONTRIBUTORS.md b/fastapi_log_mail/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..6e720b67d --- /dev/null +++ b/fastapi_log_mail/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- [PyTech](https://www.pytech.it): + - Simone Rubino \<\> diff --git a/fastapi_log_mail/readme/DESCRIPTION.md b/fastapi_log_mail/readme/DESCRIPTION.md new file mode 100644 index 000000000..e92d7f261 --- /dev/null +++ b/fastapi_log_mail/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module allows to create an activity when an exception is logged in a fastapi endpoint. diff --git a/fastapi_log_mail/static/description/index.html b/fastapi_log_mail/static/description/index.html new file mode 100644 index 000000000..026bfe3b8 --- /dev/null +++ b/fastapi_log_mail/static/description/index.html @@ -0,0 +1,434 @@ + + + + + +FastAPI Log notification + + + +
+

FastAPI Log notification

+ + +

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

+

This module allows to create an activity when an exception is logged in +a fastapi endpoint.

+

Table of contents

+ +
+

Configuration

+

Configure a fastapi endpoint as explained in api_log_mail.

+
+
+

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

+
    +
  • PyTech
  • +
+
+
+

Contributors

+ +
+
+

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:

+

SirPyTech

+

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_mail/tests/__init__.py b/fastapi_log_mail/tests/__init__.py new file mode 100644 index 000000000..0d3e465bc --- /dev/null +++ b/fastapi_log_mail/tests/__init__.py @@ -0,0 +1 @@ +from . import test_fastapi_log_mail diff --git a/fastapi_log_mail/tests/test_fastapi_log_mail.py b/fastapi_log_mail/tests/test_fastapi_log_mail.py new file mode 100644 index 000000000..c4e83cb02 --- /dev/null +++ b/fastapi_log_mail/tests/test_fastapi_log_mail.py @@ -0,0 +1,84 @@ +# Copyright 2025 Akretion (http://www.akretion.com). +# @author Florian Mounier +# Copyright 2025 Simone Rubino - PyTech +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import os +import unittest + +from odoo.addons.fastapi.schemas import DemoExceptionType +from odoo.addons.fastapi_log.tests.common import Common +from odoo.addons.mail.tests.common import MailCase + +from fastapi import status + + +@unittest.skipIf(os.getenv("SKIP_HTTP_CASE"), "TestFastapiLogMail skipped") +class TestFastapiLogMail(Common, MailCase): + def _set_mail_exception_activity_type(self, app): + app.api_log_mail_exception_activity_type_id = app.env[ + "mail.activity.type" + ].create( + { + "name": "Test exception activity type", + "res_model": "api.log", + } + ) + + def _set_mail_exception_template(self, app): + app.api_log_mail_exception_template_id = app.env["mail.template"].create( + { + "name": "Test exception email template", + "model_id": app.env.ref("api_log.model_api_log").id, + } + ) + + def test_endpoint_exception_create_activity(self): + """If an endpoint has an activity type, + when an exception occurs an activity of the configured type is created. + """ + # Arrange + app = self._get_log_env_records(self.fastapi_demo_app) + self._set_mail_exception_activity_type(app) + activity_type = app.api_log_mail_exception_activity_type_id + route = ( + "/fastapi_demo/test/demo/exception?" + f"exception_type={DemoExceptionType.user_error.value}" + "&error_message=An error happened" + ) + # pre-condition + self.assertTrue(activity_type) + + # Act + with self.log_capturer() as capturer: + response = self.url_open(route, timeout=200) + + # Assert + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + log = capturer.records + self.assertEqual(len(log), 1) + self.assertTrue(log.activity_ids) + + def test_endpoint_exception_send_email(self): + """If an endpoint has an email template, + when an exception occurs an email is sent using the configured template. + """ + # Arrange + app = self._get_log_env_records(self.fastapi_demo_app) + self._set_mail_exception_template(app) + mail_template = app.api_log_mail_exception_template_id + route = ( + "/fastapi_demo/test/demo/exception?" + f"exception_type={DemoExceptionType.user_error.value}" + "&error_message=An error happened" + ) + # pre-condition + self.assertTrue(mail_template) + + # Act + with self.mock_mail_gateway(): + self.url_open(route, timeout=200) + + # Assert + sent_email = self._filter_mail() + self.assertTrue(sent_email) diff --git a/fastapi_log_mail/views/fastapi_endpoint_views.xml b/fastapi_log_mail/views/fastapi_endpoint_views.xml new file mode 100644 index 000000000..6e1d27886 --- /dev/null +++ b/fastapi_log_mail/views/fastapi_endpoint_views.xml @@ -0,0 +1,34 @@ + + + + + Add log mail fields to endpoint form view + fastapi.endpoint + + + + + + + + + diff --git a/setup/api_log/odoo/addons/api_log b/setup/api_log/odoo/addons/api_log new file mode 120000 index 000000000..bcddf69a4 --- /dev/null +++ b/setup/api_log/odoo/addons/api_log @@ -0,0 +1 @@ +../../../../api_log \ No newline at end of file diff --git a/setup/api_log/setup.py b/setup/api_log/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/api_log/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/setup/api_log_mail/odoo/addons/api_log_mail b/setup/api_log_mail/odoo/addons/api_log_mail new file mode 120000 index 000000000..987cf27bc --- /dev/null +++ b/setup/api_log_mail/odoo/addons/api_log_mail @@ -0,0 +1 @@ +../../../../api_log_mail \ No newline at end of file diff --git a/setup/api_log_mail/setup.py b/setup/api_log_mail/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/api_log_mail/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) 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, +) diff --git a/setup/fastapi_log_mail/odoo/addons/fastapi_log_mail b/setup/fastapi_log_mail/odoo/addons/fastapi_log_mail new file mode 120000 index 000000000..0708fcac1 --- /dev/null +++ b/setup/fastapi_log_mail/odoo/addons/fastapi_log_mail @@ -0,0 +1 @@ +../../../../fastapi_log_mail \ No newline at end of file diff --git a/setup/fastapi_log_mail/setup.py b/setup/fastapi_log_mail/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/fastapi_log_mail/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)