Skip to content

DataDog Formatter

CLASS DESCRIPTION
DatadogLogFormatter

DatadogLogFormatter

DatadogLogFormatter(
    json_serializer: (
        Callable[[LogRecord], str] | None
    ) = None,
    json_deserializer: (
        Callable[[dict | str | bool | int | float], str]
        | None
    ) = None,
    json_default: Callable[[Any], Any] | None = None,
    datefmt: str | None = None,
    use_datetime_directive: bool = False,
    log_record_order: list[str] | None = None,
    utc: bool = False,
    use_rfc3339: bool = True,
    **kwargs
)

Bases: LambdaPowertoolsFormatter

Changes compared to the default Logger Formatter:

  • timestamp format to use RFC3339 e.g., "2023-05-01T15:34:26.841+0200"
PARAMETER DESCRIPTION
log_record_order

description, by default None

TYPE: list[str] | None DEFAULT: None

PARAMETER DESCRIPTION
json_serializer

function to serialize obj to a JSON formatted str, by default json.dumps

TYPE: Callable DEFAULT: None

json_deserializer

function to deserialize str, bytes, bytearraycontaining a JSON document to a Pythonobj`, by default json.loads

TYPE: Callable DEFAULT: None

json_default

function to coerce unserializable values, by default str

Only used when no custom JSON encoder is set

TYPE: Callable DEFAULT: None

datefmt

String directives (strftime) to format log timestamp.

See https://docs.python.org/3/library/time.html#time.strftime or

TYPE: str DEFAULT: None

use_datetime_directive

Interpret datefmt as a format string for datetime.datetime.strftime, rather than time.strftime - Only useful when used alongside datefmt.

See https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior . This also supports a custom %F directive for milliseconds.

TYPE: bool DEFAULT: False

log_record_order

set order of log keys when logging, by default ["level", "location", "message", "timestamp"]

TYPE: list DEFAULT: None

utc

set logging timestamp to UTC, by default False to continue to use local time as per stdlib

TYPE: bool DEFAULT: False

use_rfc3339

Whether to use a popular dateformat that complies with both RFC3339 and ISO8601. e.g., 2022-10-27T16:27:43.738+02:00.

TYPE: bool DEFAULT: True

kwargs

Key-value to persist in all log messages

DEFAULT: {}

Source code in aws_lambda_powertools/logging/formatters/datadog.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def __init__(
    self,
    json_serializer: Callable[[LogRecord], str] | None = None,
    json_deserializer: Callable[[dict | str | bool | int | float], str] | None = None,
    json_default: Callable[[Any], Any] | None = None,
    datefmt: str | None = None,
    use_datetime_directive: bool = False,
    log_record_order: list[str] | None = None,
    utc: bool = False,
    use_rfc3339: bool = True,  # NOTE: The only change from our base formatter
    **kwargs,
):
    """Datadog formatter to comply with Datadog log parsing

    Changes compared to the default Logger Formatter:

    - timestamp format to use RFC3339 e.g., "2023-05-01T15:34:26.841+0200"


    Parameters
    ----------
    log_record_order : list[str] | None, optional
        _description_, by default None

    Parameters
    ----------
    json_serializer : Callable, optional
        function to serialize `obj` to a JSON formatted `str`, by default json.dumps
    json_deserializer : Callable, optional
        function to deserialize `str`, `bytes`, bytearray` containing a JSON document to a Python `obj`,
        by default json.loads
    json_default : Callable, optional
        function to coerce unserializable values, by default str

        Only used when no custom JSON encoder is set

    datefmt : str, optional
        String directives (strftime) to format log timestamp.

        See https://docs.python.org/3/library/time.html#time.strftime or
    use_datetime_directive: str, optional
        Interpret `datefmt` as a format string for `datetime.datetime.strftime`, rather than
        `time.strftime` - Only useful when used alongside `datefmt`.

        See https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior . This
        also supports a custom %F directive for milliseconds.

    log_record_order : list, optional
        set order of log keys when logging, by default ["level", "location", "message", "timestamp"]

    utc : bool, optional
        set logging timestamp to UTC, by default False to continue to use local time as per stdlib
    use_rfc3339: bool, optional
        Whether to use a popular dateformat that complies with both RFC3339 and ISO8601.
        e.g., 2022-10-27T16:27:43.738+02:00.
    kwargs
        Key-value to persist in all log messages
    """
    super().__init__(
        json_serializer=json_serializer,
        json_deserializer=json_deserializer,
        json_default=json_default,
        datefmt=datefmt,
        use_datetime_directive=use_datetime_directive,
        log_record_order=log_record_order,
        utc=utc,
        use_rfc3339=use_rfc3339,
        **kwargs,
    )