Skip to content

Commit 754a2a2

Browse files
committed
fix: add missing reserved log attrs
1 parent 46913ba commit 754a2a2

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

aws_lambda_powertools/logging/formatter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"name",
1212
"msg",
1313
"args",
14+
"level",
1415
"levelname",
1516
"levelno",
1617
"pathname",
@@ -29,6 +30,8 @@
2930
"processName",
3031
"process",
3132
"asctime",
33+
"location",
34+
"timestamp",
3235
)
3336

3437

@@ -187,7 +190,7 @@ def _extract_log_keys(self, log_record: logging.LogRecord) -> Dict[str, Any]:
187190
# then replace any std log attribute e.g. '%(level)s' to 'INFO', '%(process)d to '4773'
188191
# lastly add or replace incoming keys (those added within the constructor or .structure_logs method)
189192
for key, value in self.log_format.items():
190-
if isinstance(value, str) and value.startswith("%("):
193+
if value and key in RESERVED_LOG_ATTRS:
191194
formatted_log[key] = value % record_dict
192195
else:
193196
formatted_log[key] = value

tests/functional/test_logger_powertools_formatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def test_log_custom_formatting(stdout, service_name):
168168
log_dict: dict = json.loads(stdout.getvalue())
169169

170170
# THEN the `location` and "timestamp" should match the formatting
171-
assert log_dict["location"] == "[test_log_custom_formatting] test_aws_lambda_logging"
171+
assert log_dict["location"] == "[test_log_custom_formatting] test_logger_powertools_formatter"
172172
assert log_dict["timestamp"] == "fake-datefmt"
173173

174174

0 commit comments

Comments
 (0)