Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Add to Django's Default Logging?

Tags:

logging

django

I'm using Django 1.6 and would like to add to—not replace—Django's default logging. Specifically, I want to make it so that in addition to Django's default logging behavior, any log record with a log level of DEBUG or higher gets written to a log file that gets automatically rotated.

Based on what I've found through searches and my own experimenting, it seems like you have to redefine all of the logging (Django's defaults plus my rotating file logging), even when using 'disable_existing_loggers': False. I figure I'm probably doing something wrong.

I'd like to see an example of what to put in my settings.py file that will allow me to accomplish this.

like image 633
Nick Avatar asked Aug 21 '14 19:08

Nick


People also ask

How do I add a logger to Django project?

By default, the LOGGING setting is merged with Django's default logging configuration using the following scheme. If the disable_existing_loggers key in the LOGGING dictConfig is set to True (which is the dictConfig default if the key is missing) then all loggers from the default configuration will be disabled.

What is the default logging level in Python?

The default level is WARNING , which means that only events of this level and above will be tracked, unless the logging package is configured to do otherwise. Events that are tracked can be handled in different ways. The simplest way of handling tracked events is to print them to the console.


1 Answers

Based on your comments I tried this. It seems to do what you're asking.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'rotate': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '/tmp/debug.log',
        }
    },
    'loggers': {
        'django': {
            'handlers': ['rotate'],
            'propagate': True,
            'level': 'DEBUG',
        }
    },
}
like image 81
dwurf Avatar answered Nov 15 '22 05:11

dwurf