Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Django logging also if DEBUG is True

By reading the official django documentation I haven't understood much about it. https://docs.djangoproject.com/en/dev/topics/logging/#configuring-logging

I would like to enable logging also if DEBUG in settings.py is set to True. I would like the errors to be logged in a file.

How to do that?

These are the default django settings for logging which I currently have now:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

PS: I'm using Apache + mod_wsgi in my development environment because I use a development machine which i access remotely on my LAN, this means i'm not using the django development server and I can't see the console log messages.

like image 458
nemesisdesign Avatar asked Oct 11 '12 13:10

nemesisdesign


People also ask

What does Django debug true do?

One of the main features of debug mode is the display of detailed error pages. If your app raises an exception when DEBUG is True, Django will display a detailed traceback, including a lot of metadata about your environment, such as all the currently defined Django settings (from settings.py).

How do you disable verbose error handling and logging in Django?

How do you disable verbose error handling and logging in Django? If you don't want to configure logging at all (or you want to manually configure logging using your own approach), you can set LOGGING_CONFIG to None . This will disable the configuration process for Django's default logging.


1 Answers

Django logging is not disabled by default in either DEBUG mode unless you have set it to.

Add below to your handlers part of LOGGING

'file':
        {
            'level':
                'INFO',
            'class':
                'logging.FileHandler',
            'formatter':
                'verbose',
            'filename':
                'myapp.log'

        }

It will log to myapp.log file in your project root. You can specify a complete path.

And add a formatters field to the Logging dict

'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format':
                '%(levelname)s %(message)s'
        },
    },
like image 94
Pratik Mandrekar Avatar answered Oct 03 '22 18:10

Pratik Mandrekar