Skip to main content

A simple Django app to enable audit logging

Project description

For the Python only implementation (which is used by this library) see https://github.com/Amsterdam/python-audit-log

DataPunt Django Audit Log

DataPunt Audit Log is a simple Django app that will log all incoming requests and their corresponding responses to a configurable endpoint.

During the process request phase, the logger is attached to the request. Before returning a response the app can easily provide extra context. In the process response phase the audit_log middleware will send the log.

Quick start

  1. Install using pip

    pip install django_audit_log
    
  2. Add "django_audit_log" to your INSTALLED_APPS:

    INSTALLED_APPS = [
        ...
        'django_audit_log',
    ]
    
  3. Add the AuditLogMiddleware to your MIDDLEWARE:

    MIDDLEWARE = [
        ...
       'django_audit_log.middleware.AuditLogMiddleware',
    ]
    

At this point all requests/responses will be logged. For providing extra context (which you are strongly urged to do so), see next chapters.

Default context info

By default the audit log sends the following json structure per request:

{
  "http_request": {
    "method": "get|post|head|options|etc..",
    "url": "https://datapunt.amsterdam.nl?including=querystring",
    "user_agent": "full browser user agent"
  },
  "http_response": {
    "status_code": "http status code",
    "reason": "http status reason",
    "headers": {
      "key": "value"
    }
  },
  "user": {
    "authenticated": "True/False",
    "provider": "auth backend the user authenticated with",
    "realm": "optional realm when using keycloak or another provider",
    "email": "email of logged in user",
    "roles": "roles attached to the logged in user",
    "ip": "ip address"
  }
}

Each json entry is set by its corresponding method. In this case, the middleware sets them automatically by calling set_http_request() and set_user_fom_request() in the process_request method. In the process_response method the last data is set by invoking set_http_response().

After the response has been processed the middleware automatically creates the log item by calling send_log().

Custom optional context info

Per request it is possible to add optional context info. For a complete audit log, you are strongly urged to add more info inside your view.

Adding extra context is quite simple. The audit_log object has been added to the request by the middleware. Therefore every view can simply access it via the request object.

Filter

AuditLog.set_filter(self, object_name, fields, terms) allows to provide info on the requested type of object and the filters that have been used (a user searches for 'terms', which are matched on specific 'fields' of the 'object').

This method will add the following details to the log:

"filter": {
      "object": "Object name that is requested",
      "fields": "Fields that are being filtered on, if applicable",
      "terms": "Search terms, if applicable"
  }

Results

AuditLog.set_results(self, results) allows to pass a json dict detailing exactly what results have been returned to the user.

It is up to the developer to decide whether the amount of data that would be added here will become a burden instead of a blessing.

This method will add the following details to the log:

"results": {
    ...
  }

Message and loglevel

At last, a log message and loglevel can be provided to indicate what the request is actually doing. This is done by calling one of the following methods:

AuditLog.debug(self, msg)
AuditLog.info(self, msg)
AuditLog.warning(self, msg)
AuditLog.error(self, msg)
AuditLog.critical(self, msg)

These methods will add the following details to the log:

"type": "DEBUG|INFO|WARNING|ERROR|etc",
"message": "log message"

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

datapunt-django-audit-log-0.1.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

datapunt_django_audit_log-0.1.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file datapunt-django-audit-log-0.1.0.tar.gz.

File metadata

  • Download URL: datapunt-django-audit-log-0.1.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for datapunt-django-audit-log-0.1.0.tar.gz
Algorithm Hash digest
SHA256 21060cb7c727f804141630664fb6303debd136190aebe2d2bbd00b74c0171a7d
MD5 df9c28c275a415d70f3cf5c417ce7dfa
BLAKE2b-256 82fd72b34109b5537a004b05f5d0e9d11b69f03d64f38a994e93e8d35023de1f

See more details on using hashes here.

File details

Details for the file datapunt_django_audit_log-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: datapunt_django_audit_log-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for datapunt_django_audit_log-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d07177741cfed5f9c06b75dff410a46a84cfe5a13ca55a43fcf7beda6d7872d0
MD5 255f59ef21ffb50ad750c1517bff655b
BLAKE2b-256 51f6f2fcdcbde9c4a1a3a3c23fe58f2f100275ef65249852615e007c5f1b468c

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