diff --git a/README.md b/README.md index a379e6e..8302709 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ Libraries that must be added in ``requirements.txt``: ``` boto==2.42.0 redis==2.10.5 +python-json-logger==0.1.5 ``` ### Server Environment @@ -75,6 +76,13 @@ Besides, the * `ODOO_SESSION_REDIS_PREFIX`: `-odoo-test` * `ODOO_SESSION_REDIS_EXPIRATION`: `86400` (1 day) +### JSON Logging + +At least on production and integration, activate: +* `ODOO_LOGGING_JSON`: 1 +* Add ``logging_json`` in the ``server_wide_modules`` option in the + configuration file + ### Automatic Configuration Calling `ctx.env['cloud.platform'].install_exoscale()` in an diff --git a/cloud_platform/__openerp__.py b/cloud_platform/__openerp__.py index 16fdaf1..5f22e49 100644 --- a/cloud_platform/__openerp__.py +++ b/cloud_platform/__openerp__.py @@ -13,6 +13,7 @@ 'attachment_s3', 'session_redis', 'monitoring_status', + 'logging_json', #'monitoring_log_requests', 'server_environment', # OCA/server-tools ], diff --git a/logging_json/README.rst b/logging_json/README.rst new file mode 100644 index 0000000..4d60884 --- /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 0000000..a50f468 --- /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 0000000..ec4bd22 --- /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', + '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 0000000..2f91915 --- /dev/null +++ b/logging_json/json_log.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- + +import logging +import os +import threading + +from distutils.util import strtobool + +from pythonjsonlogger import jsonlogger + + +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