fix(batch): add optional logger injection for BatchProcessors (#7553)#8272
fix(batch): add optional logger injection for BatchProcessors (#7553)#8272Sujit-1509 wants to merge 8 commits into
Conversation
leandrodamascena
left a comment
There was a problem hiding this comment.
Hey @Sujit-1509 thanks for working on this. I just left some reviews before we approve and merge.
|
|
||
| # Log with full traceback when a customer-provided logger is present | ||
| # and the exception carries a real traceback (e.g. not a synthetic FIFO circuit-breaker) | ||
| batch_logger = self.logger | ||
| if batch_logger is not None and exception[2] is not None: | ||
| # ExceptionInfo allows None on every slot, but logging.warning's exc_info | ||
| # requires a fully populated tuple. We already excluded synthetic exceptions | ||
| # (no traceback) above, so the type and value are guaranteed to be set. | ||
| assert exception[0] is not None | ||
| assert exception[1] is not None | ||
| exc_info = cast("tuple[type[BaseException], BaseException, TracebackType]", exception) | ||
| batch_logger.warning( | ||
| "Record processing exception; skipping this record", | ||
| exc_info=exc_info, | ||
| ) |
There was a problem hiding this comment.
| # Log with full traceback when a customer-provided logger is present | |
| # and the exception carries a real traceback (e.g. not a synthetic FIFO circuit-breaker) | |
| batch_logger = self.logger | |
| if batch_logger is not None and exception[2] is not None: | |
| # ExceptionInfo allows None on every slot, but logging.warning's exc_info | |
| # requires a fully populated tuple. We already excluded synthetic exceptions | |
| # (no traceback) above, so the type and value are guaranteed to be set. | |
| assert exception[0] is not None | |
| assert exception[1] is not None | |
| exc_info = cast("tuple[type[BaseException], BaseException, TracebackType]", exception) | |
| batch_logger.warning( | |
| "Record processing exception; skipping this record", | |
| exc_info=exc_info, | |
| ) | |
| if self.logger is not None and exception[2] is not None: | |
| self.logger.warning( | |
| "Record processing exception; skipping this record", | |
| exc_info=exception, | |
| ) |
Can you try this pls? I think we can simplify this block of code.
| event_type: EventType, | ||
| model: BatchTypeModels | None = None, | ||
| raise_on_entire_batch_failure: bool = True, | ||
| logger: logging.Logger | Logger | None = None, |
There was a problem hiding this comment.
| logger: logging.Logger | Logger | None = None, | |
| logger: logging.Logger | None = None, |
Powertools Logger inherits from logging.Logger, so, this is not necessary here.
| raise_on_entire_batch_failure: bool | ||
| Raise an exception when the entire batch has failed processing. | ||
| When set to False, partial failures are reported in the response | ||
| logger: logging.Logger | Logger | None |
There was a problem hiding this comment.
| logger: logging.Logger | Logger | None | |
| logger: logging.Logger | None |
| self, | ||
| model: BatchSqsTypeModel | None = None, | ||
| skip_group_on_error: bool = False, | ||
| logger: logging.Logger | Logger | None = None, |
There was a problem hiding this comment.
| logger: logging.Logger | Logger | None = None, | |
| logger: logging.Logger | None = None, |
Powertools Logger inherits from logging.Logger, so, this is not necessary here.
| skip_group_on_error: bool | ||
| Determines whether to exclusively skip messages from the MessageGroupID that encountered processing failures | ||
| Default is False. | ||
| logger: logging.Logger | Logger | None |
There was a problem hiding this comment.
| logger: logging.Logger | Logger | None | |
| logger: logging.Logger | None |
| import logging | ||
|
|
||
| from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, process_partial_response |
There was a problem hiding this comment.
Please move imports to the top of the file. No need for lazy imports in tests.
| import logging | ||
|
|
||
| from aws_lambda_powertools.utilities.batch import SqsFifoPartialProcessor, process_partial_response |
There was a problem hiding this comment.
Please move imports to the top of the file. No need for lazy imports in tests.
| import logging | ||
|
|
||
| from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, process_partial_response |
There was a problem hiding this comment.
Please move imports to the top of the file. No need for lazy imports in tests.
|
|
@leandrodamascena I pushed the review updates for this PR. Addressed in
I also reran local verification for the updated branch:
Thanks again for the review. |



Issue number: closes #7553
Summary
Closes #7553.
Changes
This PR adds optional logger injection to the Batch Processing utility, allowing record-processing exceptions to be logged with their original traceback when desired.
Implementation
Added an optional \logger: Logger | logging.Logger | None\ parameter to the batch processor hierarchy.
Updated \ailure_handler()\ to emit a warning with \exc_info\ when:
Preserved existing behavior when no logger is supplied.
Added regression tests covering:
Why logger injection?
This follows existing Powertools dependency-injection patterns and avoids introducing additional boolean configuration flags or undocumented APIs.
User Experience
Before:
After:
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.