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://codecov.io/gh/sondrelg/django-js-logger/branch/master/graph/badge.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 Django app for logging Javascript’s console.log logs to Django.

Useful for catching Javascript errors that are not logged by Django natively and would otherwise only be logged to the client’s console.

The app works by posting all logs to an internal API; because of this, it should likely not be run in performance-sensitive production environments.

Flowchart

If you want to implement this package, and require extra functionality please feel free to commit an issue or a PR. Otherwise, this currently does exactly what I need it to do, and I likely won’t change it.

Quick start

  1. Add “js_logger” to your INSTALLED_APPS settings:

    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 readCookie(name) {
            let nameEQ = name + "=";
            let ca = document.cookie.split(';');
            for (let i = 0; i < ca.length; i++) {
                let c = ca[i];
                while (c.charAt(0) === ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
            }
            return null;
        }
        const CSRFToken = readCookie('csrftoken');
    
        function post(type, msg) {
            /* Sends logs to a backend API, which logs the message to a python logger */
            fetch('/js-logs/', {
                    method: 'POST',
                    headers: {'X-CSRFToken': CSRFToken, 'Content-Type': 'application/json'},
                    body: JSON.stringify({'type': type, 'msg': msg})
                }
            );
        }
    
        // Event listener for error events for posting errors to the backend
        window.addEventListener('error', (event) => {
            post('error', event.message);
        });
    
        // Patch console.log function to post all other logs to the backend
        console._overwritten = console.log;
        console.log = function (log) {
            post('info', log);
            console._overwritten(log);
        }
    </script>
  4. Include the template where ever you wish for logs to be sent in from (perhaps a base or header template):

    <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.

0.1.0 (20-03-2020)

  • First public release.

  • Provides full Django-logging for javascript loggers and errors.

  • Implements CSRFToken header.

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.1.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

django_js_logger-0.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-js-logger-0.1.0.tar.gz
  • Upload date:
  • Size: 8.5 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.1.0.tar.gz
Algorithm Hash digest
SHA256 9784a274185d85f5071d2bea527f90cc65635bb63ec13f23e79a88913d95a4e0
MD5 70db51587ecb0d8233ccccf8d3313c83
BLAKE2b-256 3f5f93aec8fe58abd71808978b1a8137f76cb25b4b834f3f84112d50352e5a8a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_js_logger-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f0e18b3965521040280c3b45af11ee20d872e7100e9437a832bb7ac508407eb
MD5 3f682eedc3f1f7cf8c86583a36a6abc2
BLAKE2b-256 19d7cd9ab95555150bd66ac512f33ce866cbc64fdd6cc3a1483d6a620fe03a52

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