ECS log formatter for Django
Project description
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 for kubi_ecs_logger https://github.com/kumina/kubi_ecs_logger for base ECS formatting
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
The formatter checks the setting LOG_SENSITIVE_USER_DATA to see if user information should be logged. If this is not set to true, only the user’s id is logged.
The Django configuration file logged is determined by running:
os.getenv('DJANGO_SETTINGS_MODULE')
You can set which formatter maps to which Django log by setting the ECS_FORMATTERS settings variable.
This defaults to:
ECS_FORMATTERS = {
"root": ECSSystemFormatter,
"django.request": ECSRequestFormatter,
"django.db.backends": ECSSystemFormatter,
}
And can be used to wire up custom formatters (see next section).
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
This can then be wired up to the list of ECS formatters used (see documentation of ECS_FORMATTERS for more information).
Tests
$ pip install -r requirements.txt
$ tox
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
Hashes for django_log_formatter_ecs-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22cbc12fbb0aa8b5be99b18b732601c6fa26e9c28edb14c62e1b35fb2dbc0730 |
|
MD5 | 8e81eca785f2a53b85109213eb20b24a |
|
BLAKE2b-256 | d850e83c04e38db4978271a3409b38030a53db960275610ffac9f7ad81fd9daf |
Hashes for django_log_formatter_ecs-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4052d492c4972a97ccc18aa832cc84e0514fde6560de422b21d8b0c99fe01c5f |
|
MD5 | ad0dc45695629cba9cc894ba144ac651 |
|
BLAKE2b-256 | ee5939278a405fd40e00929f3926607de4b5d5a37b677f0f8b3d4d7e0537dc3a |