Categories
Python Answers

How to setup of Python logging in Django?

Spread the love

Sometimes, we want to setup of Python logging in Django.

In this article, we’ll look at how to setup of Python logging in Django.

How to setup of Python logging in Django?

To setup of Python logging in Django, we set the LOGGING variable in settings.py.

For instance, we write

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

in settings.py to set the settings for logging.

handlers is a dict with the settings for logging various kinds of events.

And we add the handler names into the handlers setting in loggers.

class is the path to the logger class.

level is the logging level.

Conclusion

To setup of Python logging in Django, we set the LOGGING variable in settings.py.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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