Skip to main content

A module that provides extras for logger.

Project description

Table of Contents generated with DocToc

Logger extras for simplified structured logging.

django-logger-extra is a collection of logger extras that makes structured logging setup easier.

Adding django-logger-extra your Django project

Add django-logger-extra in your project's dependencies.

Adding django-resilient-logger Django apps

To install this logger, add INSTALLED_APPS in settings.py:

INSTALLED_APPS = (
    'logger_extra',
    ...
)

Configuring logger formatter in settings.py:

To make use of the JSON formatter it must be configured in setting's LOGGING section. Filter is optional and brings logger_context() contexts available as logger extras.

LOGGING = {
    'filters': {
        'context': {
            '()': 'logger_extra.filter.LoggerContextFilter',
        }
        ...
    },
    'formatters': {
        'json': {
            '()': 'logger_extra.formatter.JSONFormatter',
        }
        ...
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'json',
            'filters': ['context']
        },
        ...
    },
    ...
}

Configuring middleware in settings.py:

MIDDLEWARE = [
    'logger_extra.middleware.XRequestIdMiddleware',
    ...
]

django-auditlog extra context.

This library can also augment django-auditlog's additional_data fields with active context. To enable this, optional package django-auditlog must be installed and it must be explicitly enabled in settings.py file.

LOGGER_EXTRA_AUGMENT_DJANGO_AUDITLOG = True

Logger context usage

Active context can be appended with logger_context function. It will return current context as resource. The current context can also be read using function get_logger_context.

import logging
from django.http import HttpRequest, JsonResponse

from logger_extra.logger_context import logger_context

logger = logging.getLogger("audit")

def bar():
  with logger_context({ "who": "World" }) as ctx:
    logger.info(f"{ctx['greet']} {ctx['who']}")

def foo():
  with logger_context({ "greet": "Hello" }):
    bar()
    return JsonResponse({})

Will result log entry that looks like: {"message": "Hello World", "level": "INFO", "time": "2025-04-14T11:08:22.962222+00:00", "context": {"request_id": "95e787b5-4ce8-46ef-bb6e-31651fc8774b", "greet": "Hello", "who": "World"}}

Development

Virtual Python environment can be used. For example:

python3 -m venv .venv
source .venv/bin/activate

Install package requirements:

pip install -e .

Install development requirements:

pip install -r requirements-test.txt

Running tests

pytest

Code format

This project uses Ruff for code formatting and quality checking.

Basic ruff commands:

  • lint: ruff check
  • apply safe lint fixes: ruff check --fix
  • check formatting: ruff format --check
  • format: ruff format

pre-commit can be used to install and run all the formatting tools as git hooks automatically before a commit.

Git blame ignore refs

Project includes a .git-blame-ignore-revs file for ignoring certain commits from git blame. This can be useful for ignoring e.g. formatting commits, so that it is more clear from git blame where the actual code change came from. Configure your git to use it for this project with the following command:

git config blame.ignoreRevsFile .git-blame-ignore-revs

Commit message format

New commit messages must adhere to the Conventional Commits specification, and line length is limited to 72 characters.

When pre-commit is in use, commitlint checks new commit messages for the correct format.

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_logger_extra-1.0.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

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

django_logger_extra-1.0.0-py2.py3-none-any.whl (8.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django_logger_extra-1.0.0.tar.gz.

File metadata

  • Download URL: django_logger_extra-1.0.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_logger_extra-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4ab52de7fa4863c60f42e7c809911c342650b0556eb48989d0b98b23efc4bfc1
MD5 530b2dce647063511a3390d15d980ce6
BLAKE2b-256 241907a007b08b80528baca2018be2d51e48bcc883ad14f322f55aaf83772d63

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_logger_extra-1.0.0.tar.gz:

Publisher: publish.yml on City-of-Helsinki/django-logger-extra

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_logger_extra-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_logger_extra-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 665396ed30959e258c93bfb3aad84b4f3a6072cd9fad703d76f2960d63b25201
MD5 31e15a4e8c2e3d7ef12b98249abc2021
BLAKE2b-256 43de7233af2856cb13a831f13cd444dd37185167d7e797c7679e12bfaa256fb4

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_logger_extra-1.0.0-py2.py3-none-any.whl:

Publisher: publish.yml on City-of-Helsinki/django-logger-extra

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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