Formats Django logs in ECS format.
Project description
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
-
Clone the repository:
git clone https://github.com/uktrade/django-log-formatter-ecs.git && cd django-log-formatter-ecs
-
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.
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
Built Distribution
File details
Details for the file django_log_formatter_ecs-0.0.6.tar.gz
.
File metadata
- Download URL: django_log_formatter_ecs-0.0.6.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.0 Darwin/23.0.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a902a69a1fb30af6d7329616c31dfc7ffaf50aecc75dbb9f3db60819be265b9e |
|
MD5 | 209d3680088ffc53aca6dd1e1bc3e318 |
|
BLAKE2b-256 | 5a855dd9a86881c6540a25786d997c91710553c36ece59cd1f75d664724604f6 |
File details
Details for the file django_log_formatter_ecs-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: django_log_formatter_ecs-0.0.6-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.0 Darwin/23.0.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 172b3c3b34315263d9ad05f320054f4b6ef6b7c38c16638672dd546c1df6afad |
|
MD5 | 84158309bfd1bd3bed44862c5cdc7ac1 |
|
BLAKE2b-256 | 87d17787812264e443dde749df159d129aeaeed77d2bd50d4608046403a787bd |