Django – verbose console logging in development server

The moment you set DEBUG = False in development server for Django, you will be clueless about what’s going on for every error your application may encounter. If you just want to show verbose error messages on the console (when you run the manage.py built-in web server for Django, follow the simple steps below.

In your settings.py, the default logging configuration may be configured for I don’t know, is it production ready? For Django starters, this will hide basic errors you may encounter. We are going to configure the LOGGING dict to have the required formatters, handlers and loggers. Below is my LOGGING section of settings.py.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s',
        },
        'simple': {
            'format': '%(levelname)s %(message)s',
        },
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'django.request': {
            # 'handlers': ['mail_admins'],
            'handlers': ['console'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

Please note that this is for development mode only and for those who run the build-in web server. For production purpose, change the settings for the appropriate logging.

This entry was posted in Python and tagged , , , . Bookmark the permalink.

Related Posts

One Response to Django – verbose console logging in development server

  1. tom says:

    LOGGING = {
    ‘version’: 1,
    ‘disable_existing_loggers’: False,
    ‘filters’: {
    ‘require_debug_false’: {
    ‘()’: ‘django.utils.log.ReuireDebugFalse’
    }

    Hi
    That is my code, but i dont get it work, any idea??

Leave a Reply

Your email address will not be published. Required fields are marked *