Browse Source

[IMP] add regex to parse logging message to extract information from it and set it in a new fields, update README file

pull/777/head
Sébastien BEAU 8 years ago
parent
commit
cb7060a115
  1. 61
      logging_json/README.rst
  2. 2
      logging_json/__openerp__.py
  3. 47
      logging_json/json_log.py

61
logging_json/README.rst

@ -1,10 +1,16 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
==============
JSON Logging
============
==============
This addon allows to output the Odoo logs in JSON.
Configuration
-------------
=============
The json logging is activated with the environment variable
``ODOO_LOGGING_JSON`` set to ``1``.
@ -12,3 +18,54 @@ The json logging is activated with the environment variable
In order to have the logs from the start of the server, you should add
``logging_json`` in the ``--load`` flag or in the ``server_wide_modules``
option in the configuration file.
To configure this module, you need to:
Known issues / Roadmap
======================
* Completed the extraction (in regex) of interesting message to get more metric
Bug Tracker
===========
Bugs are tracked on `GitHub Issues
<https://github.com/OCA/server-tools/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smash it by providing detailed and welcomed feedback.
Credits
=======
Images
------
* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.
Contributors
------------
* Guewen Baconnier <guewen.baconnier@camptocamp.com>
* Sébastien BEAU <sebastien.beau@akretion.com>
Funders
-------
The development of this module has been financially supported by:
* Camptocamp
* Akretion
Maintainer
----------
.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org
This module is maintained by the OCA.
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.
To contribute to this module, please visit https://odoo-community.org.

2
logging_json/__openerp__.py

@ -4,7 +4,7 @@
{'name': 'JSON Logging',
'version': '9.0.1.0.0',
'author': 'Camptocamp,Odoo Community Association (OCA)',
'author': 'Camptocamp,Akretion,Odoo Community Association (OCA)',
'license': 'AGPL-3',
'category': 'Extra Tools',
'depends': ['base',

47
logging_json/json_log.py

@ -1,8 +1,14 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Camptocamp (http://www.camptocamp.com).
# @author Guewen Baconnier <guewen.baconnier@camptocamp.com>
# Copyright 2017 Akretion (http://www.akretion.com).
# @author Sébastien BEAU <sebastien.beau@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
import os
import threading
import re
from distutils.util import strtobool
@ -18,6 +24,34 @@ except ImportError:
def is_true(strval):
return bool(strtobool(strval or '0'.lower()))
# The following regex are use to extract information from native odoo log
# We struct is the following
# {name_of_the_log : [regex, formatter, extra_vals]}
# The extra_vals "dict" will be merged into the jsonlogger if the regex match
# In order to make understandable the regex please add an string example that
# match
REGEX = {
'openerp.addons.base.ir.ir_cron': [
# "cron.object.execute('db', 1, '*', u'base.action.rule', u'_check')"
("cron\.object\.execute\('.+'(?P<cron_model>[\w.]+).+"
"'(?P<cron_method>[\w.]+)'\)",
{}, {'cron_running': True}),
# "0.016s (base.action.rule, _check)"
("(?P<cron_duration_second>[\d.]+)s \("
"(?P<cron_model>[\w.]+), (?P<cron_method>[\w.]+)",
{'cron_duration_second': float},
{'cron_running': False, 'cron_status': 'succeeded'}),
# Call of self.pool.get('base.action.rule')._check(
# cr, uid, *u'()') failed in Job 43
("Call of self\.pool\.get\('(?P<cron_model>[\w.]+)'\)"
".(?P<cron_method>[\w.]+)",
{}, {'cron_running': False, 'cron_status': 'failed'}),
],
}
class OdooJsonFormatter(jsonlogger.JsonFormatter):
@ -25,7 +59,18 @@ class OdooJsonFormatter(jsonlogger.JsonFormatter):
record.pid = os.getpid()
record.dbname = getattr(threading.currentThread(), 'dbname', '?')
_super = super(OdooJsonFormatter, self)
return _super.add_fields(log_record, record, message_dict)
res = _super.add_fields(log_record, record, message_dict)
if log_record['name'] in REGEX:
for regex, formatters, extra_vals in REGEX[log_record['name']]:
match = re.match(regex, log_record['message'])
if match:
vals = match.groupdict()
for key, func in formatters.items():
vals[key] = func(vals[key])
log_record.update(vals)
if extra_vals:
log_record.update(extra_vals)
return res
if is_true(os.environ.get('ODOO_LOGGING_JSON')):

Loading…
Cancel
Save