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',
...
},
},
"root": {
"handlers": ["asim"],
...
}
"loggers": {
"django": {
"handlers": ["asim"],
"propagate": False
...
},
},
}
In this example we assign the ASIM formatter to a handler
and ensure both root
and django
loggers use this handler
. We then set propagate
to False
on the django
logger, to avoid duplicating logs at the root level.
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.
Publishing
- Acquire API token from Passman.
- Request access from the SRE team.
- Note: You will need access to the
platform
group in Passman.
- 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.
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_asim-0.0.6.tar.gz
.
File metadata
- Download URL: django_log_formatter_asim-0.0.6.tar.gz
- Upload date:
- Size: 5.3 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 | ba35df7a8c5f5bad2e006b55dea8e339ef82f7476a93e6cae4d2bbea12938c2b |
|
MD5 | e3a3eb086329af91e2d06bc214c9a8c9 |
|
BLAKE2b-256 | be9f4f97297df96f69fa0a6e1d8930e2c57bf22f180abb99374372c6db32c539 |
File details
Details for the file django_log_formatter_asim-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: django_log_formatter_asim-0.0.6-py3-none-any.whl
- Upload date:
- Size: 5.9 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 | 00923cb95b599082d94895af1333d1bb8fce05a3d742dd571d32b7e0e480ead3 |
|
MD5 | 44638a2d8102450e2d402280907f7823 |
|
BLAKE2b-256 | e5f0855339857db39f7e619a942b6d8f3d4ef8ba1a24b8ec558c12879e12444c |