Formatter
CLASS | DESCRIPTION |
---|---|
BasePowertoolsFormatter |
|
LambdaPowertoolsFormatter |
Powertools for AWS Lambda (Python) Logging formatter. |
BasePowertoolsFormatter ¶
Bases: Formatter
METHOD | DESCRIPTION |
---|---|
clear_state |
Removes any previously added logging keys |
thread_safe_clear_keys |
Removes any previously added logging keys in a specific thread |
clear_state
abstractmethod
¶
clear_state() -> None
Removes any previously added logging keys
Source code in aws_lambda_powertools/logging/formatter.py
61 62 63 64 |
|
thread_safe_clear_keys ¶
thread_safe_clear_keys() -> None
Removes any previously added logging keys in a specific thread
Source code in aws_lambda_powertools/logging/formatter.py
81 82 83 |
|
LambdaPowertoolsFormatter ¶
LambdaPowertoolsFormatter(
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 = False,
serialize_stacktrace: bool = True,
**kwargs
)
Bases: BasePowertoolsFormatter
Powertools for AWS Lambda (Python) Logging formatter.
Formats the log message as a JSON encoded string. If the message is a dict it will be used directly.
The log_record_order
kwarg is used to specify the order of the keys used in
the structured json logs. By default the order is: "level", "location", "message", "timestamp",
"service".
Other kwargs are used to specify log field format strings.
PARAMETER | DESCRIPTION |
---|---|
json_serializer
|
function to serialize
TYPE:
|
json_deserializer
|
function to deserialize
TYPE:
|
json_default
|
function to coerce unserializable values, by default str Only used when no custom JSON encoder is set
TYPE:
|
datefmt
|
String directives (strftime) to format log timestamp. See https://docs.python.org/3/library/time.html#time.strftime or
TYPE:
|
use_datetime_directive
|
Interpret See https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior . This also supports a custom %F directive for milliseconds.
TYPE:
|
utc
|
set logging timestamp to UTC, by default False to continue to use local time as per stdlib
TYPE:
|
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:
|
log_record_order
|
set order of log keys when logging, by default ["level", "location", "message", "timestamp"]
TYPE:
|
kwargs
|
Key-value to be included in log messages
DEFAULT:
|
METHOD | DESCRIPTION |
---|---|
append_context_keys |
Context manager to temporarily add logging keys. |
format |
Format logging record as structured JSON str |
serialize |
Serialize structured log dict to JSON str |
Source code in aws_lambda_powertools/logging/formatter.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
append_context_keys ¶
append_context_keys(
**additional_keys: Any,
) -> Generator[None, None, None]
Context manager to temporarily add logging keys.
PARAMETER | DESCRIPTION |
---|---|
**additional_keys
|
Key-value pairs to include in the log context during the lifespan of the context manager.
TYPE:
|
Example
1 2 3 4 |
|
Source code in aws_lambda_powertools/logging/formatter.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
|
format ¶
format(record: logging.LogRecord) -> str
Format logging record as structured JSON str
Source code in aws_lambda_powertools/logging/formatter.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
serialize ¶
serialize(log: LogRecord) -> str
Serialize structured log dict to JSON str
Source code in aws_lambda_powertools/logging/formatter.py
186 187 188 |
|