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 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 ("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
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
Hashes for django_log_formatter_ecs-0.0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 116d98d61b407dbff6654c7854072d6f3ab519bfc4718330e7aa91d07d17dfc3 |
|
MD5 | e03152a72f219e216740a038b5b0c15e |
|
BLAKE2b-256 | 8516d20accf8e5d18e84fd61283ce4c5a71ef2dbb9e2e14d8b1d13060869f448 |