Skip to main content

Django ECS log formatter

The library formats Django logs in ECS format.

https://www.elastic.co/guide/en/ecs/current/index.html

Mapping to the format is incomplete and best effort has been made to create logical field mappings between Django and ECS.

If you need to amend the mapping you can implement a custom formatter (see below).

Installation

pip install django-log-formatter-ecs

Usage

Using in a Django logging configuration:

from django_log_formatter_ecs import ECSFormatter

LOGGING = {
    ...
    "formatters": {
        "ecs_formatter": {
            "()": ECSFormatter,
        },
    },
    'handlers': {
        'ecs': {
            'formatter': 'ecs_formatter',
            ...
        },
    },
    "loggers": {
        "django": {
            "handlers": ["ecs"],
            ...
        },
    },
}

Dependencies

This package uses kubi_ecs_logger https://github.com/kumina/kubi_ecs_logger for base ECS formatting

This package uses Django IPware https://github.com/un33k/django-ipware for IP address capture.

This package is compatible with django-user_agents https://pypi.org/project/django-user-agents/ which, when used, will enhance logged user agent information.

Settings

DLFE_APP_NAME - used to define the application name that should be logged.

DLFE_LOG_SENSITIVE_USER_DATA - the formatter checks this setting to see if user information should be logged. If this is not set to true, only the user's id is logged.

DLFE_ZIPKIN_HEADERS - used for defining custom zipkin headers, the defaults is :code:("X-B3-TraceId" "X-B3-SpanId")

The Django configuration file logged is determined by running:

os.getenv('DJANGO_SETTINGS_MODULE')

Formatter classes

    ECS_FORMATTERS = {
        "root": ECSSystemFormatter,
        "django.request": ECSRequestFormatter,
        "django.db.backends": ECSSystemFormatter,
    }

The default class for other loggers is:

    ECSSystemFormatter

Creating a custom formatter

If you wish to create your own ECS formatter, you can inherit from ECSSystemFormatter and call _get_event_base to get the base level logging data for use in augmentation:

    class ECSSystemFormatter(ECSFormatterBase):
        def get_event(self):
            logger_event = self._get_event_base()

            # Customise logger event

            return logger_event

Contributing to the django-log-formatter-ecs package

Getting started

  1. Clone the repository:

    git clone https://github.com/uktrade/django-log-formatter-ecs.git && cd django-log-formatter-ecs
    
  2. Install the required dependencies:

    pip install poetry && poetry install && poetry run pre-commit install
    

Testing

Automated testing

Run poetry run pytest in the root directory to run all tests.

Or, run poetry run tox in the root directory to run all tests for multiple Python versions. See the tox configuration file.

Publishing

  1. Acquire API token from Passman.
    • Request access from the SRE team.
    • Note: You will need access to the platform group in Passman.
  2. Run poetry config pypi-token.pypi <token> to add the token to your Poetry configuration.

Update the version, as the same version cannot be published to PyPI.

poetry version patch

More options for the version command can be found in the Poetry documentation. For example, for a minor version bump: poetry version minor.

Build the Python package.

poetry build

Publish the Python package.

Note: Make sure your Pull Request (PR) is approved and contains the version upgrade in pyproject.toml before publishing the package.

poetry publish

Check the PyPI Release history to make sure the package has been updated.

For an optional manual check, install the package locally and test everything works as expected.

Metadata

Release files for django-log-formatter-ecs 0.0.7

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-log-formatter-ecs 0.0.7
File Size Uploaded Provenance
django_log_formatter_ecs-0.0.7.tar.gz 4.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-log-formatter-ecs 0.0.7
File Interpreter ABI Platform
django_log_formatter_ecs-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 10.6 kB

Release files / django_log_formatter_ecs-0.0.7.tar.gz

Download URL django_log_formatter_ecs-0.0.7.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6b60c84d6f170c906dc3821f40cb6b7a395ff71c050fba83c063d93a93ee1932
BLAKE2b-256 checksum
How to use checksums
d49db5947cd9c14426584ae1586c971bb5977f5357339bf2a37eef57e0ea5925
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.11.5 Darwin/23.5.0

Release files / django_log_formatter_ecs-0.0.7-py3-none-any.whl

Download URL django_log_formatter_ecs-0.0.7-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f0ba3df16f6bcfc7723d6567c39604333604e61d606355eaf44072be9ab88e6
BLAKE2b-256 checksum
How to use checksums
e9d7f274b8b7aa49656b47542ace1a2b91c4ec6dba8b35346b9c7fe983496c9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.11.5 Darwin/23.5.0

Security for django-log-formatter-ecs 0.0.7

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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