File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed
aws_lambda_powertools/logging Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -152,7 +152,11 @@ def format(self, record: logging.LogRecord) -> str: # noqa: A003
152152 """Format logging record as structured JSON str"""
153153 formatted_log = self ._extract_log_keys (log_record = record )
154154 formatted_log ["message" ] = self ._extract_log_message (log_record = record )
155- formatted_log ["exception" ], formatted_log ["exception_name" ] = self ._extract_log_exception (log_record = record )
155+ # exception and exception_name fields can be added as extra key
156+ # in any log level, we try to extract and use them first
157+ extracted_exception , extracted_exception_name = self ._extract_log_exception (log_record = record )
158+ formatted_log ["exception" ] = formatted_log .get ("exception" , extracted_exception )
159+ formatted_log ["exception_name" ] = formatted_log .get ("exception_name" , extracted_exception_name )
156160 formatted_log ["xray_trace_id" ] = self ._get_latest_trace_id ()
157161 formatted_log = self ._strip_none_records (records = formatted_log )
158162
Original file line number Diff line number Diff line change @@ -251,6 +251,19 @@ def test_logger_append_duplicated(stdout, service_name):
251251 assert "new_value" == log ["request_id" ]
252252
253253
254+ def test_logger_honors_given_exception_keys (stdout , service_name ):
255+ # GIVEN Logger is initialized with exception and exception_name fields
256+ logger = Logger (service = service_name , stream = stdout )
257+
258+ # WHEN log level info
259+ logger .info ("log" , exception = "exception_value" , exception_name = "exception_name_value" )
260+
261+ # THEN log statements should have these keys
262+ log = capture_logging_output (stdout )
263+ assert "exception_value" == log ["exception" ]
264+ assert "exception_name_value" == log ["exception_name" ]
265+
266+
254267def test_logger_invalid_sampling_rate (service_name ):
255268 # GIVEN Logger is initialized
256269 # WHEN sampling_rate non-numeric value
You can’t perform that action at this time.
0 commit comments