Skip to content
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

[16.0] [ADD] fastapi_log #501

Draft
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions fastapi_log/README.rst
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.
2 changes: 2 additions & 0 deletions fastapi_log/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import fastapi_dispatcher
23 changes: 23 additions & 0 deletions fastapi_log/__manifest__.py
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",
}
76 changes: 76 additions & 0 deletions fastapi_log/fastapi_dispatcher.py
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()

Check warning on line 39 in fastapi_log/fastapi_dispatcher.py

View check run for this annotation

Codecov / codecov/patch

fastapi_log/fastapi_dispatcher.py#L39

Added line #L39 was not covered by tests

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

Check warning on line 46 in fastapi_log/fastapi_dispatcher.py

View check run for this annotation

Codecov / codecov/patch

fastapi_log/fastapi_dispatcher.py#L45-L46

Added lines #L45 - L46 were not covered by tests

log = env["fastapi.log"].log_request(
request, environ, fastapi_endpoint.id
)
except Exception as e:
_logger.warning("Failed to log request", exc_info=e)

Check warning on line 52 in fastapi_log/fastapi_dispatcher.py

View check run for this annotation

Codecov / codecov/patch

fastapi_log/fastapi_dispatcher.py#L51-L52

Added lines #L51 - L52 were not covered by tests

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)

Check warning on line 60 in fastapi_log/fastapi_dispatcher.py

View check run for this annotation

Codecov / codecov/patch

fastapi_log/fastapi_dispatcher.py#L59-L60

Added lines #L59 - L60 were not covered by tests
raise e
else:
try:
log and log.log_response(response)
except Exception as e:
_logger.warning("Failed to log response", exc_info=e)

Check warning on line 66 in fastapi_log/fastapi_dispatcher.py

View check run for this annotation

Codecov / codecov/patch

fastapi_log/fastapi_dispatcher.py#L65-L66

Added lines #L65 - L66 were not covered by tests
finally:
if not tools.config["test_enable"]:
try:
cr.commit() # pylint: disable=E8102

Check warning on line 70 in fastapi_log/fastapi_dispatcher.py

View check run for this annotation

Codecov / codecov/patch

fastapi_log/fastapi_dispatcher.py#L69-L70

Added lines #L69 - L70 were not covered by tests
finally:
cr.close()

Check warning on line 72 in fastapi_log/fastapi_dispatcher.py

View check run for this annotation

Codecov / codecov/patch

fastapi_log/fastapi_dispatcher.py#L72

Added line #L72 was not covered by tests
return response

else:
return super().dispatch(endpoint, args)
2 changes: 2 additions & 0 deletions fastapi_log/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import fastapi_endpoint
from . import fastapi_log
34 changes: 34 additions & 0 deletions fastapi_log/models/fastapi_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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", store=True
)

@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)
Loading