Skip to main content

Adds information about requests to logging records.

Project description

This package provides a logging filter and middleware to add information about the current request to the logging record.

Installation and Usage

Install the package, add django_requestlogging to settings.INSTALLED_APPS, add django_requestlogging.middleware.LogSetupMiddleware to settings.MIDDLEWARE_CLASSES, and add django_requestlogging.logging_filters.RequestFilter as a logging filter. See below for an example logging configuration.

The filter adds information about the current request to the logging record. The following keys can be substituted in the logging formatter string:


The user agent string, provided by the client.


The requested HTTP path.


The remote IP address.


The HTTP request method (e.g. GET, POST, PUT, DELETE, etc.)


The server protocol (e.g. HTTP, HTTPS, etc.)


The username for the logged-in user.

If any of this information cannot be extracted from the current request (or there is no current request), a hyphen '-' is substituted as a placeholder.

Logging Configuration Example

This logging configuration can be added to your DJANGO_SETTINGS_MODULE. It adds an unbound RequestFilter, which will be bound to the current request by the middleware and then unbound again at response time.

    'filters': {
        # Add an unbound RequestFilter.
        'request': {
            '()': 'django_requestlogging.logging_filters.RequestFilter',
    'formatters': {
        'request_format': {
            'format': '%(remote_addr)s %(username)s "%(request_method)s '
            '%(path_info)s %(server_protocol)s" %(http_user_agent)s '
            '%(message)s %(asctime)s',
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'filters': ['request'],
            'formatter': 'request_format',
    'loggers': {
        'myapp': {
            # Add your handlers that have the unbound request filter
            'handlers': ['console'],
            # Optionally, add the unbound request filter to your
            # application.
            'filters': ['request'],

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-requestlogging-1.0.1.tar.gz (7.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page