This library provides the ability of converting django logs to JSON.
Using standard Json library:
pip install git+https://github.com/missterr/django-json-logging.git
To use Orjson:
pip install git+https://github.com/missterr/django-json-logging.git#egg=django-json-logging[orjson]
and add to Django settings LOGGING_SERIALIZER="orjson"
To use Ujson:
pip install git+https://github.com/missterr/django-json-logging.git#egg=django-json-logging[ujson]
and add to Django settings LOGGING_SERIALIZER="ujson"
Note: If you're using zsh you need to escape square brackets: pip install ... [ujson]
# setting.py
LOGGING_APP_NAME = 'your_app_name' # required
LOGGING_FIELDS = ('levelname', 'name', 'module', 'process', 'thread', 'pathname', 'asctime')
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'json': {
'()': 'django_json_logging.formatters.JSONFormatter',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'json',
},
},
'loggers': {
'': {
'handlers': ['console'],
'level': 'WARNING',
},
'django': {
'handlers': ['console'],
'level': 'WARNING',
'propagate': False,
},
},
}
Somewhere in the code
import logging
your_json_logger = logging.getLogger('json_logger')
your_json_logger.warning('Something happened here', extra={'event_code': 'xxx'})