Skip to content

fix: Logger string interpolation to main reserved keys only #53

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 4 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion aws_lambda_powertools/helper/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def build_lambda_context_model(context: object) -> LambdaContextModel:

context = {
"function_name": context.function_name,
"function_memory_size": str(context.memory_limit_in_mb),
"function_memory_size": context.memory_limit_in_mb,
"function_arn": context.invoked_function_arn,
"function_request_id": context.aws_request_id,
}
Expand Down
18 changes: 10 additions & 8 deletions aws_lambda_powertools/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(self, **kwargs):
datefmt = kwargs.pop("datefmt", None)

super(JsonFormatter, self).__init__(datefmt=datefmt)
self.reserved_keys = ["timestamp", "level", "location"]
self.format_dict = {
"timestamp": "%(asctime)s",
"level": "%(levelname)s",
Expand All @@ -76,10 +77,12 @@ def format(self, record): # noqa: A003

log_dict = {}
for key, value in self.format_dict.items():
if value:
if value and key in self.reserved_keys:
# converts default logging expr to its record value
# e.g. '%(asctime)s' to '2020-04-24 09:35:40,698'
log_dict[key] = value % record_dict
else:
log_dict[key] = value

if isinstance(record_dict["msg"], dict):
log_dict["message"] = record_dict["msg"]
Expand Down Expand Up @@ -149,20 +152,19 @@ def handler(evt, ctx):
raise DeprecationWarning("Use Logger instead - This method will be removed when GA")


def _is_cold_start() -> str:
"""Verifies whether is cold start and return a string used for struct logging
def _is_cold_start() -> bool:
"""Verifies whether is cold start

Returns
-------
str
lower case bool as a string
aws_lambda_logging doesn't support bool; cast cold start value to string
bool
cold start bool value
"""
cold_start = "false"
cold_start = False

global is_cold_start
if is_cold_start:
cold_start = str(is_cold_start).lower()
cold_start = is_cold_start
is_cold_start = False

return cold_start
Expand Down
8 changes: 4 additions & 4 deletions tests/functional/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,12 @@ def handler(event, context):
first_log, second_log, third_log, fourth_log = logs

# First execution
assert "true" == first_log["cold_start"]
assert "true" == second_log["cold_start"]
assert first_log["cold_start"] is True
assert second_log["cold_start"] is True

# Second execution
assert "false" == third_log["cold_start"]
assert "false" == fourth_log["cold_start"]
assert third_log["cold_start"] is False
assert fourth_log["cold_start"] is False


def test_log_metric(capsys):
Expand Down