How can I add Django to the default log?

I am using Django 1.6 and would like to add to-not replace- Django default logging . In particular, I want to make sure that in addition to the default Django log behavior, any log entry with a log level of DEBUG or higher is written to a log file that is automatically rotated.

Based on the search results and my own experiments, it seems that you need to override all logging (by default Django plus my log with a rotating file) even when using 'disable_existing_loggers': False . I suppose I'm probably something wrong.

I would like to see an example of what to put in my settings.py file, which will allow me to accomplish this.

+5
source share
3 answers

settings.py is just Python, so we can do:

 from django.utils.log import DEFAULT_LOGGING # Use defaults as the basis for our logging setup LOGGING = DEFAULT_LOGGING # We need some formatters. These ones are from the docs. LOGGING['formatters'] = { 'verbose': { 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s' }, 'simple': { 'format': '%(levelname)s %(message)s' }, } # A log file handler that rotates periodically LOGGING['handlers'].update({ 'rotate': { 'level': 'DEBUG', 'class': 'logging.handlers.RotatingFileHandler', 'formatter': 'verbose', 'filename': '/tmp/debug.log', } }) # The default 'django' logger is a catch-all that does nothing. We replace it with # a rotating file handler. LOGGING['loggers'].update({ 'django': { 'handlers': ['rotate'], 'propagate': True, 'level': 'DEBUG', } }) # If you don't want to completely replace the django handler, you could do something # like this instead: #LOGGING['loggers']['django']['handlers'] += ['rotate'] 

This will add your rotating file handler to existing handlers, identify the basic formatting elements, and replace the logger of all traps (which does nothing) with the one that replaces the default logger

+1
source

Based on your comments, I tried this. He seems to be doing what you ask.

 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', } }, } 
+1
source

Source: https://habr.com/ru/post/1200873/


All Articles