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

[MIG] 17.0 - smile_log - from Inafon migration #131

Open
wants to merge 1 commit into
base: 17.0
Choose a base branch
from
Open
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
135 changes: 135 additions & 0 deletions smile_log/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
====================
Logging in database
====================

.. |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-Smile_SA%2Fodoo_addons-lightgray.png?logo=github
:target: https://github.com/Smile-SA/odoo_addons/tree/17.0/smile_log
:alt: Smile-SA/odoo_addons

|badge2| |badge3|

This module adds a logs handler writing to database.

Notice

* Following code will create a log in db with a unique pid per logger:
from odoo.smile_addons.smile_log.tools import SmileDBLogger
logger = SmileDBLogger(self._cr.dbname, model'res.partner', self.id, self._uid)
logger.info(your_message)

Features :

* Create logs when executing an action.
* Archive and delete old logs from database.
* Give users access right to see logs.


**Table of contents**

.. contents::
:local:

Configuration
=============

* Developer adds ``import logging`` to his python file.
* Developer must add following code to his action and specify the database, the model name, the res_id, and uid. Then give a message to log for information:

.. code-block:: python

logger = SmileDBLogger(self._cr.dbname, model'res.partner', self.id, self._uid)
logger.info(your_message)

* Administrator must create a ``Scheduled Action`` to call the function ``archive_and_delete_old_logs``, configure archiving path and the number of days to archive and delete logs.

Usage
=====
To add Logs handler to an action :

1. Import SmileDBLogger to your python code and add code lines as shown in following example :

.. figure:: static/description/inherit_and_import_smile_log.png
:alt: Import SmileDBLogger
:width: 600px

2. Add ``smile_log`` to your module dependence:

.. figure:: static/description/manifest.png
:alt: Depends manifest
:width: 500px

3. Now execute the action.:

.. figure:: static/description/action.png
:alt: Button validate
:width: 850px

4. Go to ``Settings > Technical > Logging``> Logs menu to see logs.

.. figure:: static/description/logs.png
:alt: Logs
:width: 850px

Administrator can give access right to users, to see logs, by checking ``Smile Logs / User``.

.. figure:: static/description/smile_logs_user.png
:alt: Smile Logs
:width: 600px

To create the scheduled action:
1. Go to ``Settings > Technical > Automation > Scheduled Actions`` and fill fields as follow:

.. figure:: static/description/scheduled_action.png
:alt: Schedules Action
:width: 850px

``(Make sure that the given folder has a write access!)``

2. After running the action, the extracted logs file in csv format is as shown in next figure:

.. figure:: static/description/exported_log.png
:alt: Smile Logs
:width: 380px

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/Smile-SA/odoo_addons/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed feedback
`here <https://github.com/Smile-SA/odoo_addons/issues/new?body=module:%20smile_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.

GDPR / EU Privacy
=================
This addons does not collect any data and does not set any browser cookies.

Credits
=======

Authors
-------

* Corentin POUHET-BRUNERIE
* Xavier FERNANDEZ
* Majda EL MARIOULI
* Cédric Leroy

Maintainer
----------
This module is maintained by the Smile SA.

Since 1991 Smile has been a pioneer of technology and also the European expert in open source solutions.

.. image:: https://avatars0.githubusercontent.com/u/572339?s=200&v=4
:alt: Smile SA
:target: http://smile.fr

This module is part of the `odoo-addons <https://github.com/Smile-SA/odoo_addons>`_ project on GitHub.

You are welcome to contribute.

6 changes: 6 additions & 0 deletions smile_log/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
# (C) 2021 Smile (<http://www.smile.fr>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import models
from . import tools
30 changes: 30 additions & 0 deletions smile_log/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# (C) 2021 Smile (<http://www.smile.fr>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Logging in database",
"version": "3.0",
"author": "Smile",
"website": 'http://www.smile.fr',
"category": "Tools",
"license": 'AGPL-3',
"description": """
Logs handler writing to database

Notice

* Following code will create a log in db with a unique pid per logger:
from odoo.addons.smile_log.tools import SmileDBLogger
logger = SmileDBLogger(self._cr.dbname, model'res.partner', self.id, self._uid)
logger.info(your_message)
""",
"depends": ['base'],
"data": [
"security/smile_log_security.xml",
"security/ir.model.access.csv",
"views/smile_log_view.xml",
],
"installable": True,
"active": True,
}
111 changes: 111 additions & 0 deletions smile_log/i18n/fr.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * smile_log
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-10-29 14:15+0000\n"
"PO-Revision-Date: 2021-10-29 14:15+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: smile_log
#: model:ir.model.fields,field_description:smile_log.field_smile_log__log_date
msgid "Date"
msgstr "Date"

#. module: smile_log
#: model:ir.model.fields,field_description:smile_log.field_smile_log__display_name
msgid "Display Name"
msgstr "Afficher Nom"

#. module: smile_log
#: model_terms:ir.ui.view,arch_db:smile_log.smile_log_search_view
msgid "Group By..."
msgstr "Grouper par..."

#. module: smile_log
#: model:ir.model.fields,field_description:smile_log.field_smile_log__id
msgid "ID"
msgstr ""

#. module: smile_log
#: model:ir.model.fields,field_description:smile_log.field_smile_log____last_update
msgid "Last Modified on"
msgstr "Dernière modification le"

#. module: smile_log
#: model:ir.model.fields,field_description:smile_log.field_smile_log__level
msgid "Level"
msgstr "Niveau"

#. module: smile_log
#: model:ir.ui.menu,name:smile_log.menu_logging
msgid "Logging"
msgstr "Historisation"

#. module: smile_log
#: model:ir.actions.act_window,name:smile_log.act_smile_log
#: model:ir.ui.menu,name:smile_log.menu_smile_log
#: model_terms:ir.ui.view,arch_db:smile_log.smile_log_search_view
#: model_terms:ir.ui.view,arch_db:smile_log.smile_log_simple_tree_view
#: model_terms:ir.ui.view,arch_db:smile_log.smile_log_tree_view
msgid "Logs"
msgstr "Historiques"

#. module: smile_log
#: model:ir.model.fields,field_description:smile_log.field_smile_log__message
msgid "Message"
msgstr ""

#. module: smile_log
#: model_terms:ir.ui.view,arch_db:smile_log.smile_log_search_view
msgid "Model"
msgstr "Modèle"

#. module: smile_log
#: model:ir.model.fields,field_description:smile_log.field_smile_log__model_name
msgid "Model name"
msgstr "Nom du modèle"

#. module: smile_log
#: model_terms:ir.ui.view,arch_db:smile_log.smile_log_search_view
msgid "PID"
msgstr ""

#. module: smile_log
#: model:ir.model.fields,field_description:smile_log.field_smile_log__pid
msgid "Pid"
msgstr ""

#. module: smile_log
#: model:ir.model.fields,field_description:smile_log.field_smile_log__res_id
msgid "Ressource id"
msgstr "Id de la ressource"

#. module: smile_log
#: model:ir.model.fields,field_description:smile_log.field_smile_log__log_res_name
msgid "Ressource name"
msgstr "Nom de la ressource"

#. module: smile_log
#: model:ir.model,name:smile_log.model_smile_log
msgid "Smile Logs"
msgstr "Logs"

#. module: smile_log
#: model:res.groups,name:smile_log.group_smile_log_user
msgid "Smile Logs / User"
msgstr "Smile Logs / Utilisateur"

#. module: smile_log
#: model:ir.model.fields,field_description:smile_log.field_smile_log__log_uid
#: model:ir.model.fields,field_description:smile_log.field_smile_log__log_user_name
msgid "User"
msgstr "Utilisateur"
5 changes: 5 additions & 0 deletions smile_log/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# (C) 2021 Smile (<http://www.smile.fr>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import smile_log
64 changes: 64 additions & 0 deletions smile_log/models/smile_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
# (C) 2021 Smile (<http://www.smile.fr>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import os
import time

from odoo import api, models, fields


class SmileLog(models.Model):
_name = 'smile.log'
_description = 'Smile Logs'
_rec_name = 'message'
_log_access = False
_order = 'log_date desc'

@api.depends('log_uid')
def _get_user_name(self):
for log in self:
user = self.env['res.users'].browse(log.log_uid)
if user.exists():
log.log_user_name = "%s [%s]" % (user.name, log.log_uid)
else:
log.log_user_name = "[%s]" % log.log_uid

@api.depends('res_id')
def _get_res_name(self):
for log in self:
log.log_res_name = ""
res = self.env[log.model_name].browse(log.res_id)
infos = res.name_get()
if infos:
log.log_res_name = infos[0][1]

log_date = fields.Datetime('Date', readonly=True)
log_uid = fields.Integer('User', readonly=True)
log_user_name = fields.Char(
string='User', size=256, compute='_get_user_name')
log_res_name = fields.Char(
string='Ressource name', size=256, compute='_get_res_name')
model_name = fields.Char('Model name', size=64, readonly=True, index=True)
res_id = fields.Integer(
'Ressource id', readonly=True, group_operator="count", index=True)
pid = fields.Integer(readonly=True, group_operator="count")
level = fields.Char(size=16, readonly=True)
message = fields.Text('Message', readonly=True)

@api.model
def archive_and_delete_old_logs(self, nb_days=90, archive_path=''):
# Thanks to transaction isolation, the COPY and DELETE will find
# the same smile_log records
if archive_path:
file_name = time.strftime("%Y%m%d_%H%M%S.log.csv")
file_path = os.path.join(archive_path, file_name)
self.env.cr.execute("""COPY (SELECT * FROM smile_log
WHERE log_date + interval'%s days' < NOW() at time zone 'UTC')
TO %s
WITH (FORMAT csv, ENCODING utf8)""", (nb_days, file_path,))
self.env.cr.execute(
"DELETE FROM smile_log "
"WHERE log_date + interval '%s days' < NOW() at time zone 'UTC'",
(nb_days,))
return True
2 changes: 2 additions & 0 deletions smile_log/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
smile_log_user,smile_log group_user,model_smile_log,base.group_user,1,0,0,0
10 changes: 10 additions & 0 deletions smile_log/security/smile_log_security.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<odoo>
<data noupdate="0">

<record id="group_smile_log_user" model="res.groups">
<field name="name">Smile Logs / User</field>
</record>

</data>
</odoo>
Binary file added smile_log/static/description/action.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added smile_log/static/description/exported_log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added smile_log/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading