Skip to main content

A small package to log django requests and responses

Project description

Django Logging Middleware

The extension for django to log requests and responses using loguru.

Sometimes code in django views becomes messy because we log requests and response. Also it takes a lot of time to write logs in every view. Using this extension for django, logging becomes simple: all you need is five minutes of installing this package and setting configuration!

Requirements

  • python >=3.6
  • django >=3.0
  • django rest framework >= 3.10 (optional)

Installation

  1. Install using pip...

    pip install django-logging-middleware

  2. Add 'logging_middleware' to your INSTALLED_APPS setting.

    INSTALLED_APPS = [ 
        ... 
        'logging_middleware'
    ]
    
  3. Add setting in your settings.py:

    DJANGO_LOGGING_MIDDLEWARE = {
        'DEFAULT_FORMAT': False,
        'MESSAGE_FORMAT': "<b><green>{time}</green> <cyan>{message}</cyan></b>"
    }
    
  4. Add 'logging_middleware.middlewares.DjangoLoggingMiddleware' to your MIDDLEWARE setting.

    MIDDLEWARE = {
        ...
        'logging_middleware.middlewares.DjangoLoggingMiddleware'
    }
    

Note 'logging_middleware.middlewares.DjangoLoggingMiddleware' should be last in the list MIDDLEWARE

You are ready to see log messages like ones below!

...
2021-01-08T23:39:33.597138 Request URL: http://localhost:8000/restframework/simple/class/with_query_string/?data=data
2021-01-08T23:39:33.597530 Request METHOD: PUT
2021-01-08T23:39:33.597874 Request HEADERS: {'Cookie': '', 'Content-Length': '26', 'Content-Type': 'application/json'}
...

Configuration

If you missed 4th step in installation, default settings would be:

DJANGO_LOGGING_MIDDLEWARE = {
    'DEFAULT_FORMAT': True,
    'MESSAGE_FORMAT': "<b><green>{time}</green> <cyan>{message}</cyan></b>"
}

Settings

'DEFAULT_FORMAT'

'DEFAULT_FORMAT' by default is True. It means middleware will use default string format for log messages. By setting it to False, you should provide in 'MESSAGE_FORMAT' your own string format.

'MESSAGE_FORMAT'

'MESSAGE_FORMAT' sets the format of log messages. By default is '<b><green>{time}</green> <cyan>{message}</cyan></b>'. For making your own format string, please look the loguru docs. Note if you set 'DEFAULT_FORMAT' as True, but also set custom string format in 'MESSAGE_FORMAT', the log messages will take the default format. Also by setting 'DEFAULT_FORMAT' as False without providing the 'MESSAGE_FORMAT', the format of messages will be default.

License

MIT-License, see LICENSE file.

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-logging-middleware-0.1.2.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django-logging-middleware-0.1.2.tar.gz.

File metadata

  • Download URL: django-logging-middleware-0.1.2.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.6.9

File hashes

Hashes for django-logging-middleware-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f0def5d42d7b9eeab2c9cce8dcb15fa49035cee5edc942209c961460ea7d275d
MD5 a581b6f2494c1ff118f0ff0e1c786108
BLAKE2b-256 766e49b6c3d2824f3d9ffbd610a112c64184b6533c9d70ce67874bb6f687d006

See more details on using hashes here.

File details

Details for the file django_logging_middleware-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: django_logging_middleware-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.6.9

File hashes

Hashes for django_logging_middleware-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 12bc85946f176fbd32fb69302a10bc98b296f6b9b6413615158f109e19e8d5c3
MD5 2f1f51627812ab38015b5babbe37e1e3
BLAKE2b-256 973eb34c1b74b98d567925f151b0c96fc98874d40111293e9b2bb4a6ffca173f

See more details on using hashes here.

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