1

I want to add a global custom formatter to all django modules so I can ingest a consistent log file with Elasticsearch. How do I figure out what django modules are logging what so I can change all their formatters?

enter image description here

Here is my log config in settings.py. Using django 2.0.

# NOTE: Copied from DEFAULT_LOGGING and added custom format
# from django.utils.log import DEFAULT_LOGGING

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {'()': 'django.utils.log.RequireDebugFalse'},
        'require_debug_true': {'()': 'django.utils.log.RequireDebugTrue'}},
    'formatters': {
        'django.server': {
            '()': 'django.utils.log.ServerFormatter',
            'format': '[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(module)s %(message)s',
            'datefmt': '%Y-%m-%d %H:%M:%S'}},
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'filters': ['require_debug_true'],
            'level': 'INFO'},
        'django.server': {
            'class': 'logging.StreamHandler',
            'formatter': 'django.server',
            'level': 'INFO'},
        'mail_admins': {
            'class': 'django.utils.log.AdminEmailHandler',
            'filters': ['require_debug_false'],
            'level': 'ERROR'}},
    'loggers': {
        'api': {
            'handlers': ['django.server'],
            'level': 'INFO',
            'propagate': False},
        'django': {
            'handlers': ['django.server', 'mail_admins'],
            'level': 'INFO'},
        'django.server': {
            'handlers': ['django.server'],
            'level': 'INFO',
            'propagate': False}},
}
daino3
  • 4,386
  • 37
  • 48

1 Answers1

0

Add your unique tag in the formatting string and then search for this keywords in your ELK.

Like :

%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] MYMODULE [%(module)s ] %(message)s'

In above case I will search for MYMODULE tags in the log.

Prakash Kumar
  • 2,554
  • 2
  • 18
  • 28