Browse Source

[IMP] as jsonlogger support natively extra keys, add a customer formatter for showing them in dev mode without json log

pull/777/head
Sébastien BEAU 7 years ago
parent
commit
04d31a85cd
  1. 21
      logging_json/README.rst
  2. 23
      logging_json/json_log.py

21
logging_json/README.rst

@ -9,6 +9,20 @@ JSON Logging
This addon allows to output the Odoo logs in JSON.
It also add the possibility to log extra data in json in your log.
See documentation of json logger here : https://github.com/madzak/python-json-logger
Example:
.. code-block:: python
_logger.info('My Message', extra={
'my_extra_key_1': 'value_1',
'my_extra_key_2': 'value_2'})
The key "extra" is a standard key of logger module so you can add this key and your log will work with or without this module (see documentation here : https://docs.python.org/2/library/logging.html#logging.Logger.debug)
Configuration
=============
@ -18,7 +32,12 @@ 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:
If you want to see the extra keys when you are developping in local
but with the odoo logger output and not the JSON output
you can add the environment variable ``ODOO_LOGGING_JSON_DEV`` set to ``1``.
Note : ``ODOO_LOGGING_JSON_DEV`` and ``ODOO_LOGGING_JSON`` should be not set both to ``1``
Known issues / Roadmap
======================

23
logging_json/json_log.py

@ -9,8 +9,10 @@ import logging
import os
import threading
import re
import json
from distutils.util import strtobool
from openerp.netsvc import ColoredFormatter
_logger = logging.getLogger(__name__)
@ -73,8 +75,29 @@ class OdooJsonFormatter(jsonlogger.JsonFormatter):
return res
class OdooJsonDevFormatter(ColoredFormatter):
def format(self, record):
response = super(OdooJsonDevFormatter, self).format(record)
extra = {}
RESERVED_ATTRS = list(jsonlogger.RESERVED_ATTRS) + ["dbname", "pid"]
for key, value in record.__dict__.items():
if (key not in RESERVED_ATTRS and not
(hasattr(key, "startswith") and key.startswith('_'))):
extra[key] = value
if extra:
response += " extra: " + json.dumps(
extra, indent=4, sort_keys=True)
return response
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
elif is_true(os.environ.get('ODOO_LOGGING_JSON_DEV')):
format = ('%(asctime)s %(pid)s %(levelname)s'
'%(dbname)s %(name)s: %(message)s')
formatter = OdooJsonDevFormatter(format)
logging.getLogger().handlers[0].formatter = formatter
Loading…
Cancel
Save