-
-
Notifications
You must be signed in to change notification settings - Fork 354
[16.0] [ADD] fastapi_log #501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <https://github.com/OCA/rest-framework/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 <https://github.com/OCA/rest-framework/issues/new?body=module:%20fastapi_log%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. | ||
|
|
||
| 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 <https://odoo-community.org/page/maintainer-role>`__: | ||
|
|
||
| |maintainer-paradoxxxzero| | ||
|
|
||
| This module is part of the `OCA/rest-framework <https://github.com/OCA/rest-framework/tree/16.0/fastapi_log>`_ project on GitHub. | ||
|
|
||
| You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import models | ||
| from . import fastapi_dispatcher |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Copyright 2025 Akretion (http://www.akretion.com). | ||
| # @author Florian Mounier <florian.mounier@akretion.com> | ||
| # 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", | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Copyright 2025 Akretion (http://www.akretion.com). | ||
| # @author Florian Mounier <florian.mounier@akretion.com> | ||
| # 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want to rollback the all the process if you encounter an error only with the cursor used for logging? |
||
| finally: | ||
| cr.close() | ||
| return response | ||
|
|
||
| else: | ||
| return super().dispatch(endpoint, args) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import fastapi_endpoint | ||
| from . import fastapi_log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Copyright 2025 Akretion (http://www.akretion.com). | ||
| # @author Florian Mounier <florian.mounier@akretion.com> | ||
| # 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) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: what do you think about calling the
get_uidmethod offastapi.endpoint?This way, if we need to override the method, the same endpoint will be matched
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_uidwould return the user ID, could you please clarify how that could be used in this piece of code?In akretion#8 I have included your PR #515 and used the new
get_endpoint, is that what you meant?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's what I meant. Sorry for the confusion.