Skip to main content

Lightweight, Low-Config Logging for Django - View and Manage Log Data Conveniently

Project description

Downloads PyPI Version License: MIT Published

security: bandit Bandit Tests Coverage Linter

Python Django



Django Log Lens is a dependency-free, lightweight, and easy-to-use logging app for Django. It provides an interface to supervise log data while also serving as a useful tool for debugging. As a unique feature, it allows clients to send console logs to the server out of the box, working with any frontend framework by simply adding a single line of code to include the required script.

Want to try it out? →Get started!

Core Features

Overview of Accessible Handlers and the Corresponding Log Files

Log Lens Handler Overview


Semantically Highlighted Logs in Your Browser

Log File Demo


Fast Navigation through Log Files

Log File Navigation Demo


Fast Navigation through Source Code

  • Click on a path in the log message to copy it to the clipboard
  • Click on the button to open the referenced line in VS Code
  • Adjust the Path Splitter and the Path Prefix to match your project structure

Navigate through Source Code

Example:

Say, the remote project root is /web/my-project (as in the example above) and your local project root is /home/user/MY-PROJECT.

  • Set the Path Prefix to /home/user/MY-PROJECT
  • Set the Path Splitter to /my-project

→ Now, by clicking on the path
/web/my-project/django/dvenv/lib/python3.10/site-packages/django/http/request.py:151,
/home/user/MY-PROJECT/django/dvenv/lib/python3.10/site-packages/django/http/request.py:151
will be opened by VS Code instead.

Client Logging

Allows clients to send console logs to the server.

<!DOCTYPE html>
<html>
  ...
  <body>
    {% include 'js-logger.html' %} <!-- #1 -->
    ...
  </body>
  <script>
    throw new Error("Hello, Django Log Lens!"); // #2
  </script>
</html>
  • #1. Include the script to send console logs to the server. It will simply override the console methods (debug, info, warn...) in a way they behave the same as before but also send the logs to the server. Thus, the script does not interfere with your frontend framework and can be used out-of-the-box. You should use this only in development mode - otherwise, clients will be able to send arbitrary logs to your server. (not harmful, but may clutter your log files)
  • #2. You will find errors, including their stack trace, in a log file if you set up django log lens as described below.

Getting Started

1. Install django-log-lens from PyPI

pip install django-log-lens

2. Add django_log_lens to your INSTALLED_APPS

# file: settings.py

INSTALLED_APPS = [
    'django_log_lens',
    ...
]

3. Add URL patterns to your urls.py

# file: urls.py
from django.urls import include

urlpatterns = [
    path('logs/', include('django_log_lens.urls')),
    ...
]

4. Add a LOGGING configuration in your settings.py

All you need to configure is the LOG_FOLDER where your log files are stored which should point to an existing folder. With your existing logging configuration, you are good to go. For semantic highlighting of log levels, either use django_log_lens.LOG_FORMAT or use the django_log_lens.LEVEL_PREFIX in your own format.

Follow the instructions from the official Django documentation to configure the logging system or use the example below.

# file: settings.py
from django_log_lens import LOG_FORMAT

LOG_FOLDER = BASE_DIR / "logs"

if not os.path.exists(LOG_FOLDER):
    os.makedirs(LOG_FOLDER)

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {"default": {"format": LOG_FORMAT}},
    "handlers": {
        "log_collector": {
            "level": "WARNING",
            "class": "logging.FileHandler",
            "filename": str(LOG_FOLDER / "collector.log"),
            "formatter": "default",
        },
        "client_logger": {
            "level": "DEBUG",
            "class": "logging.FileHandler",
            "filename": str(LOG_FOLDER / "client.log"),
            "formatter": "default",
        },
    },
    "loggers": {
        "django_log_lens.client": {"handlers": ["client_logger"], "level": "DEBUG", "propagate": True},
        "django" : {"handlers": ["log_collector"], "level": "DEBUG", "propagate": True},
    }
}

ALLOW_JS_LOGGING = DEBUG # it's recommendable not to allow client logging in production

5. Visit Log Lens

You can now visit Django Log Lens by navigating to {% url 'django_log_lens:view' %} (code for your template) - if you configured the URL pattern as shown above, this would be logs/view

FAQ

  • Why is are my logs not colored according to the log level?

    Make sure to set the LOG_FORMAT in your settings.py as shown in the example above.

  • Can I use my own logging format?

    Basically, yes. Just make sure prefix the loglevel to the log message as shown in the following example:

    from django_log_lens import LEVEL_PREFIX
    
    MY_LOG_FORMAT = "%(levelname)s - %(message)s" # adjust to your needs
    MY_LOG_LENS_FORMAT = LEVEL_PREFIX + MY_LOG_FORMAT
    
  • Which handlers are recognized by Django Log Lens?

    The following handlers will be recognized automatically: FileHandler, RotatingFileHandler, TimedRotatingFileHandler, WatchedFileHandler

    As a side note, be aware that the WatchedFileHandler is inappropriate for use under windows as open files cannot be moved or renamed.

  • What if I want to use a custom handler?

    Assume you have a custom handler called CustomHandler in the file myapp/handlers.py:

    from logging.handlers import TimedRotatingFileHandler
    class CustomFileHandler(TimedRotatingFileHandler): # could also inherit from any other handler
        pass # add your custom logic here
    

    Add the following lines to the logging configuration in your settings.py:

    from django_log_lens import add_handler
    # add your custom handler by its fully qualified class name:
    add_handler("myapp.handlers.CustomFileHandler")
    

    Now, the custom handler will be recognized by Django Log Lens and you can view the logs in the web interface.

Third Party Licenses

This project uses the Dracula theme by Zeno Rocha which is licensed under the MIT License

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_log_lens-1.0.1.tar.gz (39.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_log_lens-1.0.1-py3-none-any.whl (38.1 kB view details)

Uploaded Python 3

File details

Details for the file django_log_lens-1.0.1.tar.gz.

File metadata

  • Download URL: django_log_lens-1.0.1.tar.gz
  • Upload date:
  • Size: 39.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for django_log_lens-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a8710cc4e2de7cb684baa290af4acf33f584330de66ede958f887d4254db1cbc
MD5 9ea99dfb43ac0ad61c5cf312a1976544
BLAKE2b-256 cd4c4e867936d46b895ef1bf0fd0cd5239617b60217cc49d3dcca63c06f59cff

See more details on using hashes here.

File details

Details for the file django_log_lens-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_log_lens-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 865c79217bcecc6cffbd4ba51a0edc45aac625c89fb05286e764e2890155bd61
MD5 c3d01fdcb38cc5480503b40040803420
BLAKE2b-256 f926932f25c6185bb401136100ac0941bf5fc7c1e2670de918f2e11d17eadc61

See more details on using hashes here.

Supported by

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