Skip to main content

logsetu-django

Ship Django / DRF logs and unhandled errors to your self-hosted LogSetu server. Zero runtime dependencies, non-blocking (background thread + batching), and it never crashes your app if the log server is down.

pip install logsetu-django

Setup (2 minutes)

# settings.py
INSTALLED_APPS = [..., "logsetu"]  # optional, but nice for discoverability

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "logsetu": {
            "class": "logsetu.handler.LogSetuHandler",
            "api_key": env("LOGSETU_API_KEY"),        # from the LogSetu dashboard → Projects
            "endpoint": env("LOGSETU_ENDPOINT"),      # e.g. https://logs.example.com
            "environment": env("ENVIRONMENT", default="production"),
            "source": "django-backend",
            "level": "WARNING",
        },
    },
    "root": {"handlers": ["logsetu"], "level": "INFO"},
}

MIDDLEWARE = [
    ...,
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "logsetu.middleware.LogSetuMiddleware",   # after auth middleware so user ids are captured
]

That's it. Every logging call at or above the handler's level is sent to LogSetu, and the middleware reports unhandled exceptions (and 5xx responses) with the request path, method, view, user, redacted headers and full traceback.

import logging
log = logging.getLogger(__name__)

log.warning("Checkout is slow", extra={"duration_ms": 1830, "cart_items": 12})
try:
    charge(card)
except PaymentError:
    log.exception("Payment failed")          # traceback is attached automatically

Multi-tenant apps

Tag every log with the current tenant by pointing the SDK at a callable (request) -> str | None:

# settings.py
LOGGING["handlers"]["logsetu"]["get_tenant_id"] = "myapp.tenants.get_tenant_id"

# myapp/tenants.py
def get_tenant_id(request):
    return getattr(request, "tenant", None) and request.tenant.slug

Or, from anywhere inside a request (e.g. after you resolve the tenant in your own middleware):

from logsetu import update_request_context
update_request_context(tenant_id=tenant.slug, plan=tenant.plan)

Handler options

Option Default Description
api_key / endpoint — Required (or set LOGSETU = {"API_KEY": ..., "ENDPOINT": ...} in settings)
environment "production" Tag on every log
source "django" Tag on every log; override per record with extra={"source": "celery"}
get_tenant_id None Dotted path or callable (request) -> str
include_extra True Forward extra={...} fields as metadata
include_request_context True Attach request id / path / method / tenant set by the middleware
flush_interval 2.0 Seconds between background flushes
batch_size 10 Flush as soon as this many logs are queued
max_queue_size 1000 Drop oldest logs beyond this
max_retries 3 Retries with exponential backoff on 5xx / network errors
timeout 5.0 HTTP timeout in seconds
debug False Print every transport failure to stderr (otherwise only the first)

Middleware options (settings.LOGSETU)

The middleware reuses the client and options of a configured LogSetuHandler. Without a handler it can run standalone:

LOGSETU = {
    "API_KEY": env("LOGSETU_API_KEY"),
    "ENDPOINT": env("LOGSETU_ENDPOINT"),
    "ENVIRONMENT": "production",
    "SOURCE": "django-backend",
    "GET_TENANT_ID": "myapp.tenants.get_tenant_id",
    "REDACT_HEADERS": ["authorization", "cookie", "x-api-key"],  # defaults already cover these
    "CAPTURE_5XX": True,   # also report 5xx responses that didn't raise
}

Every request gets a request_id (taken from X-Request-ID if present, otherwise generated) which is attached to all logs emitted during that request and exposed as request.logsetu_request_id.

Guarantees

  • Never blocks the request cycle: emit() only appends to an in-memory queue.
  • Never raises into your app: transport errors are printed once to stderr, then suppressed.
  • Never leaks secrets: Authorization, Cookie, X-Api-Key, X-CSRFToken and Proxy-Authorization are redacted.
  • Never loses the last logs on shutdown: the queue is flushed via atexit.

Celery / management commands

Nothing special — the handler works anywhere Python's logging does. Set extra={"source": "celery"} to distinguish workers, or configure a second handler with a different source.

License

MIT

Metadata

Release files for logsetu-django 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logsetu-django 0.1.0
File Size Uploaded
logsetu_django-0.1.0.tar.gz 15.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logsetu-django 0.1.0
File Interpreter ABI Platform
logsetu_django-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 30.0 kB

Release files / logsetu_django-0.1.0.tar.gz

Download URL logsetu_django-0.1.0.tar.gz
Size 15.0 kB
Tags Source
SHA-256 checksum
How to use checksums
52cf5e68342c6f9707c68c252a419ffe8cc24362e8aabc9bceba9ec3c56f85d9
BLAKE2b-256 checksum
How to use checksums
1f7fe62dac1c8e3ecf275c74a3ab283edb700e0d3a62839765df2be3becee48d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / logsetu_django-0.1.0-py3-none-any.whl

Download URL logsetu_django-0.1.0-py3-none-any.whl
Size 15.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4e5285d552682bba98f6c9404f3605a617ec6165e6950acab405008121850fb5
BLAKE2b-256 checksum
How to use checksums
9ad3f6a5aa8eed21fbcfd5dd9ce85ba8cb01c431955ea396c8a60c2907c00463
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

0.3.0

2 release files

0.2.0

2 release files

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page