Skip to main content

Table of Contents generated with DocToc

Logger that ensures that logs sent out to external service.

django-resilient-logger is a logger module that stores logs in local DB and synchronizes those with external log target. If for some reason synchronization to external service does not work at the given time, it will retry it at later time. Management tasks require an external cron trigger.

Cron triggers are designed to be run in following schedule:

  • submit_unsent_entries once in every 15 minutes.
  • clear_sent_entries once a month.

To manually trigger the scheduled tasks, one can run commands:

python ./manage.py submit_unsent_entries
python ./manage.py clear_sent_entries

Adding django-resilient-logger to your Django project

Add django-resilient-logger in your project"s dependencies.

Adding django-resilient-logger to Django apps

To install this logger, append resilient_logger to INSTALLED_APPS in settings.py:

INSTALLED_APPS = (
    "resilient_logger"
    ...
)

Configuring django-resilient-logger

To configure resilient logger, you must provide config section in your settings.py.

Configuration must contain required origin, environment, sources and targets keys. It also accepts optional keys batch_limit, chunk_size, clear_sent_entries and submit_unsent_entries.

  • origin is the name of the application or unique identifier of it.
  • environment is the name of the environment where the application is running.
  • sources expects array of objects with property class (full class path) being present. Other properties are ignored.
  • targets expects array of objects with class (full class path) and being present. Others are passed as constructor parameters.
RESILIENT_LOGGER = {
    "origin": "NameOfTheApplication",
    "environment": env("AUDIT_LOG_ENV"),
    "sources": [
        {"class": "resilient_logger.sources.ResilientLogSource"},
        {"class": "resilient_logger.sources.DjangoAuditLogSource"},
    ],
    "targets": [
        {
            "class": "resilient_logger.targets.ElasticsearchLogTarget",
            "es_url": env("AUDIT_LOG_ES_URL"),
            "es_username": env("AUDIT_LOG_ES_USERNAME"),
            "es_password": env("AUDIT_LOG_ES_PASSWORD"),
            "es_index": env("AUDIT_LOG_ES_INDEX"),
            "es_compress": False,
            "required": True,
        }
    ],
    "batch_limit": 5000,
    "chunk_size": 500,
    "submit_unsent_entries": True,
    "clear_sent_entries": True,
}

In addition to the django-resilient-logger specific configuration, one must also configure logger handler to actually use it. In the sample below the configured logger is called resilient and it will use the RESILIENT_LOGGER configuration above:

LOGGING = {
    "handlers": {
        "resilient": {
            "class": "resilient_logger.handlers.ResilientLogHandler",
            ...
        }
        ...
    },
    "loggers": {
        "": {
            "handlers": ["resilient"],
            ...
        },
    ...
    }
}

Workarounds

django-resilient-logger includes built-in workarounds to resolve structural and privacy issues when integrating with third-party libraries.

django-auditlog Patching

By default, django-auditlog uses smart_str (which invokes standard __str__ methods) to populate the object_repr field on LogEntry records. Because __str__ representations in models often contain sensitive personal data (e.g., names, email addresses, phone numbers), this default behavior can lead to unintentional Personally Identifiable Information (PII) leakage in audit logs.

The DjangoAuditLogEntryManager workaround intercepts log entry creation (log_create and log_m2m_changes) by temporarily monkey-patching auditlog.models.smart_str and replacing LogEntry.objects (and its associated manager references base_manager and default_manager) with a custom manager.

Configuration Settings

You can enable and configure the PII-safe representation function directly in settings.py:

# Enable/disable the django-auditlog monkey patch (Defaults to False)
RESILIENT_LOGGER_PATCH_DJANGO_AUDITLOG = True

# Custom string representation callback to sanitize or replace PII (Defaults to None)
# Accepts a dotted import path string, callable function, or None (uses default resolver)
RESILIENT_LOGGER_DJANGO_AUDITLOG_REPR_FN = "my_project.utils.pii_safe_object_repr"
  • RESILIENT_LOGGER_PATCH_DJANGO_AUDITLOG: Boolean flag. When set to True, resilient_logger automatically applies the manager patch during Django startup (AppConfig.ready()).
  • RESILIENT_LOGGER_DJANGO_AUDITLOG_REPR_FN:
    • None (or omitted): Uses the built-in resolver, which strips sensitive field data and represents Django models formatted strictly as ModelName (PK) (e.g., User (42)). Note: If the primary key itself contains PII (such as an email address), the built-in resolver will not redact it.
    • Dotted string path (e.g., "path.to.module.custom_fn"): Resolves and executes the specified custom representation callable.
    • Callable: Direct function handle (when configuring programmatically).

Manual or Custom Usage

If you need to trigger or control the patch programmatically (e.g., within isolated test cases or scripts), call DjangoAuditLogEntryManager.patch() directly.

The patch() static method returns a restore() function to safely revert LogEntry managers to their unpatched state:

from resilient_logger.workarounds.models import DjangoAuditLogEntryManager

# 1. Apply patch with a custom PII-sanitizing function or default fallback (ModelName (PK))
restore_patch = DjangoAuditLogEntryManager.patch(object_repr_fn=pii_safe_repr_fn)

try:
    # Perform operations triggering django-auditlog...
    pass
finally:
    # 2. Revert LogEntry managers back to original state
    restore_patch()

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 -e ".[all]"

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.

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.

Metadata

Release files for django-resilient-logger 3.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 django-resilient-logger 3.1.0
File Size Uploaded
django_resilient_logger-3.1.0.tar.gz 31.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-resilient-logger 3.1.0
File Interpreter ABI Platform
django_resilient_logger-3.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 60.8 kB

Release files / django_resilient_logger-3.1.0.tar.gz

Download URL django_resilient_logger-3.1.0.tar.gz
Size 31.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e770d4e58c7db7cda9f6268624069f41ae31bb0487a76cb555584b1246b93044
BLAKE2b-256 checksum
How to use checksums
c0a9ae8034664263373d813a4bef626574129771989754b7377c00014c579c10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 1, 2026.

Transparency log

Release files / django_resilient_logger-3.1.0-py2.py3-none-any.whl

Download URL django_resilient_logger-3.1.0-py2.py3-none-any.whl
Size 29.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
46369fbea1dee66b3a05f29b44a56c7c689654f97feb3b57a1e0a53491c86595
BLAKE2b-256 checksum
How to use checksums
ddca9c0cbb63293f5a5006b946bad7c3ba09ed48f091176861712248939061c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 1, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

3.1.0 This release

2 release files

3.0.0

2 release files

2.3.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.3.7

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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