Skip to content

fix(logger): support exception and exception_name fields at any log level #1930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion aws_lambda_powertools/logging/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ def format(self, record: logging.LogRecord) -> str: # noqa: A003
"""Format logging record as structured JSON str"""
formatted_log = self._extract_log_keys(log_record=record)
formatted_log["message"] = self._extract_log_message(log_record=record)
formatted_log["exception"], formatted_log["exception_name"] = self._extract_log_exception(log_record=record)
# exception and exception_name fields can be added as extra key
# in any log level, we try to extract and use them first
extracted_exception, extracted_exception_name = self._extract_log_exception(log_record=record)
formatted_log["exception"] = formatted_log.get("exception", extracted_exception)
formatted_log["exception_name"] = formatted_log.get("exception_name", extracted_exception_name)
formatted_log["xray_trace_id"] = self._get_latest_trace_id()
formatted_log = self._strip_none_records(records=formatted_log)

Expand Down
15 changes: 15 additions & 0 deletions tests/functional/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ def test_logger_append_duplicated(stdout, service_name):
assert "new_value" == log["request_id"]


def test_logger_honors_given_exception_keys(stdout, service_name):
# GIVEN Logger is initialized with exception and exception_name fields
logger = Logger(
service=service_name, stream=stdout, exception="exception_value", exception_name="exception_name_value"
)

# WHEN log level info
logger.info("log")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: add the exception fields here so it's easier to scan and understand what's being tested.

logger.info("log", exception=..., exception_name=...)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @heitorlessa! Thank you!


# THEN log statements should have these keys
log = capture_logging_output(stdout)
assert "exception_value" == log["exception"]
assert "exception_name_value" == log["exception_name"]


def test_logger_invalid_sampling_rate(service_name):
# GIVEN Logger is initialized
# WHEN sampling_rate non-numeric value
Expand Down