From 5b095c4857f8662c925c064ccba6ba704e70dff1 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Mon, 13 Mar 2017 18:54:44 +0100 Subject: [PATCH] [ADD] add module logging-json --- logging_json/README.rst | 14 ++++++++++++++ logging_json/__init__.py | 3 +++ logging_json/__openerp__.py | 18 ++++++++++++++++++ logging_json/json_log.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 logging_json/README.rst create mode 100644 logging_json/__init__.py create mode 100644 logging_json/__openerp__.py create mode 100644 logging_json/json_log.py diff --git a/logging_json/README.rst b/logging_json/README.rst new file mode 100644 index 000000000..4d60884d9 --- /dev/null +++ b/logging_json/README.rst @@ -0,0 +1,14 @@ +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``. + +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. diff --git a/logging_json/__init__.py b/logging_json/__init__.py new file mode 100644 index 000000000..a50f468c2 --- /dev/null +++ b/logging_json/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import json_log diff --git a/logging_json/__openerp__.py b/logging_json/__openerp__.py new file mode 100644 index 000000000..e73ded244 --- /dev/null +++ b/logging_json/__openerp__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +{'name': 'JSON Logging', + 'version': '9.0.1.0.0', + 'author': 'Camptocamp,Odoo Community Association (OCA)', + 'license': 'AGPL-3', + 'category': 'Extra Tools', + 'depends': ['base', + ], + 'external_dependencies': { + 'python': ['pythonjsonlogger'], + }, + 'website': 'http://www.camptocamp.com', + 'data': [], + 'installable': True, + } diff --git a/logging_json/json_log.py b/logging_json/json_log.py new file mode 100644 index 000000000..3fcc7968e --- /dev/null +++ b/logging_json/json_log.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- + +import logging +import os +import threading + +from distutils.util import strtobool + +_logger = logging.getLogger(__name__) + +try: + from pythonjsonlogger import jsonlogger +except ImportError: + jsonlogger = None # noqa + _logger.debug("Cannot 'import pythonjsonlogger'.") + + +def is_true(strval): + return bool(strtobool(strval or '0'.lower())) + + +class OdooJsonFormatter(jsonlogger.JsonFormatter): + + def add_fields(self, log_record, record, message_dict): + record.pid = os.getpid() + record.dbname = getattr(threading.currentThread(), 'dbname', '?') + _super = super(OdooJsonFormatter, self) + return _super.add_fields(log_record, record, message_dict) + + +if is_true(os.environ.get('ODOO_LOGGING_JSON')): + format = ('%(asctime)s %(pid)s %(levelname)s' + '%(dbname)s %(name)s: %(message)s') + formatter = OdooJsonFormatter(format) + logging.getLogger().handlers[0].formatter = formatter