Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure my Django log to function like the built-in development server log?

I am trying to configure my logging configuration in settings.py and there are so many options, I'm having trouble replicating the built-in development server log (that prints to console).

I want my production log to record the same information that would normally be printed to console in the development server log (GET requests, debug info, etc). I either need to know which settings I need to change below, or the location of the settings for the built-in development server log, so that I can copy that.

LOGGING = {
    'version': 1,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
            },
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/home/django/django_log.log',
            'formatter': 'simple'
            },
        },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
            },
        }
    }

if DEBUG:
    # make all loggers use the console.
    for logger in LOGGING['loggers']:
        LOGGING['loggers'][logger]['handlers'] = ['console']

I also do not want to have to add any code anywhere else but my settings.py if at all possible. I don't want to have to go into my views.py and specify what errors to print or log, I never had to do that with the development server, so I'm hoping I can figure this out.

like image 645
stephan Avatar asked Feb 03 '15 16:02

stephan


1 Answers

In Django 1.8, the default logging configuration for a debug environment is:

When DEBUG is True:

  • The django catch-all logger sends all messages at the WARNING level or higher to the console. Django doesn’t make any such logging calls at this time (all logging is at the DEBUG level or handled by the django.request and django.security loggers).
  • The py.warnings logger, which handles messages from warnings.warn(), sends messages to the console.

This logging configuration can be found at django.utils.log.DEFAULT_LOGGING. Note that the catch-all logger actually gets info messages as well, not just warning and above.

When overriding the default logging settings, note that disable_existing_loggers, if set to True, will shut up all of Django's default loggers.


The development server logs every incoming request directly to stderr like this:

[18/Oct/2015 12:08:17] "GET /about/ HTTP/1.1" 200 9946

This is specific to the development server and will not be carried over to a production environment, unless you replicate it with middleware.

like image 168
approxiblue Avatar answered Oct 28 '22 08:10

approxiblue