Skip to main content

Javascript logging for Django.

Project description

https://img.shields.io/pypi/v/django-js-logger.svg https://img.shields.io/pypi/pyversions/django-js-logger.svg https://img.shields.io/pypi/djversions/django-js-logger.svg https://img.shields.io/badge/code%20style-black-000000.svg https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white

Simple app for logging javascript console.log logs in Django.

Useful for catching javascript errors that are not logged by Django natively.

Quick start

  1. Add “js_logger” to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
        ...
        'js_logger',
    ]
  2. Include the packages URLconf in your project urls.py like this:

    path('js-logs/', include('js_logger.urls')),
  3. Add the required static file to your project by running manage.py collectstatic, or by manually adding the following code to <your-templates-folder>/js-logging/js-logging.html:

    <script type="text/javascript">
        function post(type, msg) {
            let headers;
            if (window.jsLoggerHeader) {
                headers = window.jsLoggerHeader;
            } else {
                headers = {'Content-Type': 'application/json'};
            }
            fetch('/js-logs/', {method: 'POST', headers: headers, body: JSON.stringify({'type': type, 'msg': msg})});
        }
    
        window.addEventListener('error', (event) => {
            post('error', event.message);
        });
    
        console._overwritten = console.log;
        console.log = function (log) {
            post('info', log);
            console._overwritten(log);
        }
    </script>
  4. Include the template where you wish:

    <head>
    ...
    {% include "js-logging/js-logging.html" %}
    ...
    </head>
  5. Add console.log as a logger in your logging configuration:

    'console.log': {
        'level': 'INFO',
        ...
    },

Note: This package will log all console.log calls in your frontend as INFO logs, and will log javascript errors as ERROR logs.

Custom headers

Since this package works by sending requests to your backend, you might find that it is necessary to add CSRF-tokens or other headers to make the requests work.

To set your own custom headers, simply define a variable, jsLoggerHeader that contains the headers you want to include, and this will be passed as the headers object in the requests made to the backend.

For example, in your template:

<head>
    {% include "js-logging/js-logging.html" %}
</head>
...
<script>
    ...
    let jsLoggerHeader =  {'X-CSRFToken': csrftoken, 'Content-Type': 'application/json'}
    ...
</script>


path('js-logs/', include('js.urls')),

    'console.log': {
        'handlers': default_handler,
        'level': 'DEBUG',
    },

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-js-logger-0.0.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

django_js_logger-0.0.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file django-js-logger-0.0.1.tar.gz.

File metadata

  • Download URL: django-js-logger-0.0.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for django-js-logger-0.0.1.tar.gz
Algorithm Hash digest
SHA256 09b3b11549a547921c7a04234bc14696e07e06730d8dc88fde190fed95bc6dd4
MD5 fc497c59e19a68c1a3096dea60715bd0
BLAKE2b-256 dfccaeffa644ddf4106fe3366c5c12ba2560a838113d4b74d94e9eab72eb0983

See more details on using hashes here.

File details

Details for the file django_js_logger-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_js_logger-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for django_js_logger-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f46d890441197f4797faeb937206fc5965d28552d45f79e46b35eedccffd7445
MD5 9a41a31cc19f7ee27132ebf56209188e
BLAKE2b-256 25284996fae924bd5e989845c6c0f1a8dcec4c0b9d20684d0190503e19f36246

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