Logstash formatter on stdout for Django
I wanted seemingly a simple thing: logging using a logstash formatter with extra context on a standard output inside a django application. For some reason, this wasn't obvious at all, but the solution was quite easy in the end.
Here are the steps:
- Install logstash_formatter
- add following into django settings logging:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'logstash': {
'class': 'logstash_formatter.LogstashFormatterV1',
'format': '{"extra": {"PROJECT_ID": "Gwiqie", "user": true }}',
},
},
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'logstash'
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
},
}
so quite easy in the end. I also used this approach to use logstash format only when LOGSTASH=True
environment variable is set, so I still have nice and concise logging format during development. Hence I used this:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'classic': {
'format': '[%(asctime)s] [%(levelname)s] [DJANGO] %(module)s %(message)s'
},
'logstash': {
'class': 'logstash_formatter.LogstashFormatterV1',
'format': '{"extra": {"PROJECT_ID": "Gwiqie", "user": true }}',
},
},
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'logstash' if os.environ.get('LOGSTASH', 'False') == 'True' else 'classic'
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
},
},
}