Search code examples
djangodjango-settingsdjango-logging

Django Logging Config By Apps


Django application uses logger but was not configured and I used this in
settings.py:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': LOGGING_FILE_PATH,
            'formatter': 'verbose'
        },
    },
    'loggers': {
        'django': {
            'handlers':['file'],
            'propagate': True,
            'level':'DEBUG',
        },
        'MYAPP': {
            'handlers': ['file'],
            'level': 'DEBUG',
        },
    }
}

Which I think realized Everything was being included. Including django.db.backends which shows all of the SQL queries being made. Which is useful but can be cumbersome to comb through when debugging.

How can I configure Django to filter out these loggings into specific files to make it easier to debug. There are management commands that are executed on cronjobs, I would like designate specific log files for them.


Solution

  • Create your custom handlers each with its own file path and attach loggers to them

    Some good examples with explanation you can find in same documentation