Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django logging requests

I'm using django 1.11 in aws elastic beanstalk and I've been trying to get my app to log with no luck . . .

I have these in my settings.py

LOGGING_CONFIG = None
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/opt/python/current/app/staging.log',
        },
    },
    'loggers': {
        '': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}
import logging.config
logging.config.dictConfig(LOGGING)

then I try to call one of my API and expecting something would show up in the log but it didn't. I django.request logger supposed to automatically log all incoming request or do I have to create a middleware that does logger.debug('something')?

logging in django is a lot more complicated than I thought :(

like image 324
Christian Dennis Avatar asked Aug 31 '17 03:08

Christian Dennis


People also ask

How does Django implement logging?

In order to configure logging, you use LOGGING to define a dictionary of logging settings. These settings describe the loggers, handlers, filters and formatters that you want in your logging setup, and the log levels and other properties that you want those components to have.

Where are Django logs stored?

The Django One-Click application employs Gunicorn and Upstart. Application level logging can be found in /var/log/upstart/gunicorn. log By default, Gunicorn logs to stderr and Upstart will collect output to stderr/stdout in /var/log/upstart/$JOB_NAME.

What are Middlewares in Django?

Middleware is a framework of hooks into Django's request/response processing. It's a light, low-level “plugin” system for globally altering Django's input or output. Each middleware component is responsible for doing some specific function.

What is Django admin log?

Project description Django Admin Logs is a package that allows you to either view the admin log entries from within the admin interface, or to disable them entirely.


1 Answers

Django logger extensions work differently in development than in production (settings.DEBUG=True|False).

What you expect to see - http request logs - are written by django.server logger,
which is active only in development server (runserver command).

I see that in your configuration you use django.request logger. This logger name is very confusing - because as its name suggest - users will expect it to log all http requests - but it won't.

django.request will log only 4xx and 5xx requests.

I've made a screencast which explain this issue in detail.

Django documentation about loggers.

like image 99
eugenci Avatar answered Oct 05 '22 13:10

eugenci