Formats Django logs in ASIM format.
Project description
Django ASIM log formatter
The library formats Django logs in ASIM format.
Mapping to the format may not be complete, but best effort has been made to create logical field mappings.
If you need to amend the mapping, you can implement a custom formatter.
Installation
pip install django-log-formatter-asim
Usage
Using in a Django logging configuration:
from django_log_formatter_asim import ASIMFormatter
LOGGING = {
...
"formatters": {
"asim_formatter": {
"()": ASIMFormatter,
},
},
'handlers': {
'asim': {
'formatter': 'asim_formatter',
...
},
},
"loggers": {
"django": {
"handlers": ["asim"],
...
},
},
}
Dependencies
This package uses Django IPware for IP address capture.
This package is compatible with Django User Agents which, when used, will enhance logged user agent information.
Settings
DLFA_LOG_PERSONALLY_IDENTIFIABLE_INFORMATION
- the formatter checks this setting to see if personally identifiable information should be logged. If this is not set to true, only the user's id is logged.
DLFA_TRACE_HEADERS
- used for defining custom zipkin headers, the defaults is ("X-Amzn-Trace-Id")
, but for applications hosted in GOV.UK PaaS you should use ("X-B3-TraceId", "X-B3-SpanId")
. If you are running your application in both places side by side during migration, the following should work in your Django settings:
DLFA_INCLUDE_RAW_LOG
- By default the original unformatted log is not included in the ASIM formatted log. You can enable that by setting this to True
and it will be included in AddidtionalFields.RawLog
.
from dbt_copilot_python.utility import is_copilot
if is_copilot():
DLFA_TRACE_HEADERS = ("X-B3-TraceId", "X-B3-SpanId")
Formatter classes
ASIM_FORMATTERS = {
"root": ASIMSystemFormatter,
"django.request": ASIMRequestFormatter,
}
The default class for other loggers is:
ASIMSystemFormatter
Creating a custom formatter
If you wish to create your own ASIM formatter, you can inherit from ASIMSystemFormatter and call _get_event_base to get the base level logging data for use in augmentation:
class ASIMSystemFormatter(ASIMFormatterBase):
def get_event(self):
logger_event = self._get_event_base()
# Customise logger event
return logger_event
Contributing to the django-log-formatter-asim
package
Getting started
-
Clone the repository:
git clone https://github.com/uktrade/django-log-formatter-asim.git && cd django-log-formatter-asim
-
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
Hashes for django_log_formatter_asim-0.0.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37c46a02a1e01b6837b10b399a0ea4eab4a9c4a8f1801c9fde43ee4d1045afe4 |
|
MD5 | 7ea72ab60722f945fc6d7be46db4abac |
|
BLAKE2b-256 | e44bdc1be59aab0e3d4f98e1523199540ef4f2e808917a6bf0da1e5fb46ba6b6 |
Hashes for django_log_formatter_asim-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e4c00b4367d0143b34d341ebea6da4bc3057d2055ba29f25666ff85cf939fd3 |
|
MD5 | dbaf910488e1541948f89d031e77d262 |
|
BLAKE2b-256 | 514f5087765c07fca0a62f0820c09676932b742727dcc07949c863f3adba131f |