You can use json_log_formatter. You can install it by:
pip install JSON-log-formatter==0.2.0
In django, you need to put update your logs settings like this:
LOGGING = {
...
'formatters': {
"json": {
'()': 'json_log_formatter.JSONFormatter',
}
},
...
}
And use it in your code like this:
import logging
logger_name = 'some_name'
logger = logging.getLogger(logger_name)
logger.info('Sign up', extra={'referral_code': '52d6ce'})
Here the extra parameter sent through here will be rendered in the log like this(from documentation):
{
"message": "Sign up",
"time": "2015-09-01T06:06:26.524448",
"referral_code": "52d6ce"
}
Override Json Log Formatter
You can override json_log_formatter.JSONFormatter
class to add extra info like IP address if needed. Like this:
import json_log_formatter
class CustomJsonFormatter(json_log_formatter.JSONFormatter):
def json_record(self, message, extra, record):
request = extra.pop('request', None)
if request:
extra['IP_ADDRESS'] = request.META.get('HTTP_X_FORWARDED_FOR') # or other ways to get ip
return super(CustomJsonFormatter, self).json_record(message, extra, record)
# usage
logger.info('Sign up', extra={'referral_code': '52d6ce', 'request': request }) # django request object
# Django Settings
'formatters': {
"json": {
'()': 'path.to.CustomJsonFormatter',
}