Skip to content

Commit 0d2ab65

Browse files
committed
fix: append logs when injecting lambda context #85
1 parent 0a63141 commit 0d2ab65

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Diff for: aws_lambda_powertools/logging/logger.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def decorate(event, context):
197197
lambda_context = build_lambda_context_model(context)
198198
cold_start = _is_cold_start()
199199

200-
self.structure_logs(cold_start=cold_start, **lambda_context.__dict__)
200+
self.structure_logs(append=True, cold_start=cold_start, **lambda_context.__dict__)
201201
return lambda_handler(event, context)
202202

203203
return decorate

Diff for: tests/functional/test_logger.py

+27
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,30 @@ def test_logger_invalid_sampling_rate():
256256
# THEN we should raise InvalidLoggerSamplingRateError
257257
with pytest.raises(InvalidLoggerSamplingRateError):
258258
Logger(sampling_rate="TEST")
259+
260+
261+
def test_inject_lambda_context_with_structured_log(lambda_context, stdout):
262+
# GIVEN Logger is initialized
263+
logger = Logger(stream=stdout)
264+
265+
# WHEN structure_logs has been used to add an additional key upfront
266+
# and a lambda function is decorated with logger.inject_lambda_context
267+
logger.structure_logs(append=True, additional_key="test")
268+
269+
@logger.inject_lambda_context
270+
def handler(event, context):
271+
logger.info("Hello")
272+
273+
handler({}, lambda_context)
274+
275+
# THEN lambda contextual info should always be in the logs
276+
log = capture_logging_output(stdout)
277+
expected_logger_context_keys = (
278+
"function_name",
279+
"function_memory_size",
280+
"function_arn",
281+
"function_request_id",
282+
"additional_key",
283+
)
284+
for key in expected_logger_context_keys:
285+
assert key in log

0 commit comments

Comments
 (0)