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.
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:
from dbt_copilot_python.utility import is_copilot
if is_copilot():
DLFA_TRACE_HEADERS = ("X-B3-TraceId", "X-B3-SpanId")
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 and it will be included in .
True
AddidtionalFields.RawLog
[!WARNING]
Setting DLFA_INCLUDE_RAW_LOG to True will cause additional private fields to be output to your logs.
This could include secrets, such as AWS Access Keys, private HTTP Request data, or personally identifiable information.
This setting is not recommended for a production environment.
Serialisation behaviour
The package provides one logging.Formatter class, ASIMFormatter which routes log messages to a serialiser
which generates a python dict which the formatter converts to a JSON string and prints to standard output.
It has a generic serialiser called ASIMRootFormatter and a custom serlializer for log messages where the
logger is django.request.
This serialiser outputs the following ASIM fields.
EventSchema = ProcessEvent
ActingAppType = Django
AdditionalFields[DjangoLogFormatterAsimVersion]
EventSchemaVersion
EventMessage
EventCount
EventStartTime
EventEndTime
EventType
EventResult
EventSeverity
EventOriginalSeverity
Additionally, the following DataDog fields where available:
dd.trace_id
dd.span_id
env
service
version
ASIMRequestFormatter
This serialiser outputs the following ASIM fields in addition to the ones from ASIMRootFormatter.
It is coupled to the datastructure provided by the django.request logger.
The django.request logger only outputs requests where the response code is 4xx/5xx.
SrcIpAddr and IpAddr
SrcPortNumber
SrcUserId and SrcUsername
HttpUserAgent
AdditionalFields["TraceHeaders"][trace_header_name] - See DLFA_TRACE_HEADERS setting for more information.
Creating a custom serialiser
If you wish to create your own ASIM serialiser, you can inherit from ASIMRootFormatter and call
super().get_log_dict() to get the base level logging data for augmentation: