File tree 2 files changed +28
-1
lines changed
aws_lambda_powertools/logging
2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -197,7 +197,7 @@ def decorate(event, context):
197
197
lambda_context = build_lambda_context_model (context )
198
198
cold_start = _is_cold_start ()
199
199
200
- self .structure_logs (cold_start = cold_start , ** lambda_context .__dict__ )
200
+ self .structure_logs (append = True , cold_start = cold_start , ** lambda_context .__dict__ )
201
201
return lambda_handler (event , context )
202
202
203
203
return decorate
Original file line number Diff line number Diff line change @@ -256,3 +256,30 @@ def test_logger_invalid_sampling_rate():
256
256
# THEN we should raise InvalidLoggerSamplingRateError
257
257
with pytest .raises (InvalidLoggerSamplingRateError ):
258
258
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
You can’t perform that action at this time.
0 commit comments