Skip to content

Commit

Permalink
feat(logger): log_event support event data classes (e.g. S3Event) (#984)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Brewer authored Jan 31, 2022
1 parent dea696e commit ffa4a35
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion aws_lambda_powertools/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def decorate(event, context, **kwargs):

if log_event:
logger.debug("Event received")
self.info(event)
self.info(getattr(event, "raw_event", event))

return lambda_handler(event, context)

Expand Down
19 changes: 19 additions & 0 deletions tests/functional/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from aws_lambda_powertools.logging.formatter import BasePowertoolsFormatter
from aws_lambda_powertools.logging.logger import set_package_logger
from aws_lambda_powertools.shared import constants
from aws_lambda_powertools.utilities.data_classes import S3Event, event_source


@pytest.fixture
Expand Down Expand Up @@ -635,3 +636,21 @@ def test_use_datetime(stdout, service_name, utc):
assert re.fullmatch(
f"custom timestamp: milliseconds=[0-9]+ microseconds=[0-9]+ timezone={re.escape(expected_tz)}", log["timestamp"]
)


def test_inject_lambda_context_log_event_request_data_classes(lambda_context, stdout, lambda_event, service_name):
# GIVEN Logger is initialized
logger = Logger(service=service_name, stream=stdout)

# WHEN a lambda function is decorated with logger instructed to log event
# AND the event is an event source data class
@event_source(data_class=S3Event)
@logger.inject_lambda_context(log_event=True)
def handler(event, context):
logger.info("Hello")

handler(lambda_event, lambda_context)

# THEN logger should log event received from Lambda
logged_event, _ = capture_multiple_logging_statements_output(stdout)
assert logged_event["message"] == lambda_event

0 comments on commit ffa4a35

Please sign in to comment.