|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any, Callable |
| 4 | + |
| 5 | +from aws_lambda_powertools.logging.formatter import LambdaPowertoolsFormatter |
| 6 | + |
| 7 | + |
| 8 | +class DatadogLogFormatter(LambdaPowertoolsFormatter): |
| 9 | + def __init__( |
| 10 | + self, |
| 11 | + json_serializer: Callable[[dict], str] | None = None, |
| 12 | + json_deserializer: Callable[[dict | str | bool | int | float], str] | None = None, |
| 13 | + json_default: Callable[[Any], Any] | None = None, |
| 14 | + datefmt: str | None = None, |
| 15 | + use_datetime_directive: bool = False, |
| 16 | + log_record_order: list[str] | None = None, |
| 17 | + utc: bool = False, |
| 18 | + use_rfc3339: bool = True, # NOTE: The only change from our base formatter |
| 19 | + **kwargs, |
| 20 | + ): |
| 21 | + """Datadog formatter to comply with Datadog log parsing |
| 22 | +
|
| 23 | + Changes compared to the default Logger Formatter: |
| 24 | +
|
| 25 | + - timestamp format to use RFC3339 e.g., "2023-05-01T15:34:26.841+0200" |
| 26 | +
|
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + log_record_order : list[str] | None, optional |
| 31 | + _description_, by default None |
| 32 | +
|
| 33 | + Parameters |
| 34 | + ---------- |
| 35 | + json_serializer : Callable, optional |
| 36 | + function to serialize `obj` to a JSON formatted `str`, by default json.dumps |
| 37 | + json_deserializer : Callable, optional |
| 38 | + function to deserialize `str`, `bytes`, bytearray` containing a JSON document to a Python `obj`, |
| 39 | + by default json.loads |
| 40 | + json_default : Callable, optional |
| 41 | + function to coerce unserializable values, by default str |
| 42 | +
|
| 43 | + Only used when no custom JSON encoder is set |
| 44 | +
|
| 45 | + datefmt : str, optional |
| 46 | + String directives (strftime) to format log timestamp. |
| 47 | +
|
| 48 | + See https://docs.python.org/3/library/time.html#time.strftime or |
| 49 | + use_datetime_directive: str, optional |
| 50 | + Interpret `datefmt` as a format string for `datetime.datetime.strftime`, rather than |
| 51 | + `time.strftime` - Only useful when used alongside `datefmt`. |
| 52 | +
|
| 53 | + See https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior . This |
| 54 | + also supports a custom %F directive for milliseconds. |
| 55 | +
|
| 56 | + log_record_order : list, optional |
| 57 | + set order of log keys when logging, by default ["level", "location", "message", "timestamp"] |
| 58 | +
|
| 59 | + utc : bool, optional |
| 60 | + set logging timestamp to UTC, by default False to continue to use local time as per stdlib |
| 61 | + use_rfc3339: bool, optional |
| 62 | + Whether to use a popular dateformat that complies with both RFC3339 and ISO8601. |
| 63 | + e.g., 2022-10-27T16:27:43.738+02:00. |
| 64 | + kwargs |
| 65 | + Key-value to persist in all log messages |
| 66 | + """ |
| 67 | + super().__init__( |
| 68 | + json_serializer=json_serializer, |
| 69 | + json_deserializer=json_deserializer, |
| 70 | + json_default=json_default, |
| 71 | + datefmt=datefmt, |
| 72 | + use_datetime_directive=use_datetime_directive, |
| 73 | + log_record_order=log_record_order, |
| 74 | + utc=utc, |
| 75 | + use_rfc3339=use_rfc3339, |
| 76 | + **kwargs, |
| 77 | + ) |
0 commit comments