Skip to content

Commit 5618965

Browse files
authored
Fix Logger value coercion to main keys (#53)
* fix: str formatting for reserved keys only #38 * fix: cold_start value from str to actual bool #38 * fix: memory_size value from str to actual int #38 * chore: linting
1 parent c5a9b3f commit 5618965

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

aws_lambda_powertools/helper/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def build_lambda_context_model(context: object) -> LambdaContextModel:
6565

6666
context = {
6767
"function_name": context.function_name,
68-
"function_memory_size": str(context.memory_limit_in_mb),
68+
"function_memory_size": context.memory_limit_in_mb,
6969
"function_arn": context.invoked_function_arn,
7070
"function_request_id": context.aws_request_id,
7171
}

aws_lambda_powertools/logging/logger.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __init__(self, **kwargs):
6262
datefmt = kwargs.pop("datefmt", None)
6363

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

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

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

151154

152-
def _is_cold_start() -> str:
153-
"""Verifies whether is cold start and return a string used for struct logging
155+
def _is_cold_start() -> bool:
156+
"""Verifies whether is cold start
154157
155158
Returns
156159
-------
157-
str
158-
lower case bool as a string
159-
aws_lambda_logging doesn't support bool; cast cold start value to string
160+
bool
161+
cold start bool value
160162
"""
161-
cold_start = "false"
163+
cold_start = False
162164

163165
global is_cold_start
164166
if is_cold_start:
165-
cold_start = str(is_cold_start).lower()
167+
cold_start = is_cold_start
166168
is_cold_start = False
167169

168170
return cold_start

tests/functional/test_logger.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,12 @@ def handler(event, context):
228228
first_log, second_log, third_log, fourth_log = logs
229229

230230
# First execution
231-
assert "true" == first_log["cold_start"]
232-
assert "true" == second_log["cold_start"]
231+
assert first_log["cold_start"] is True
232+
assert second_log["cold_start"] is True
233233

234234
# Second execution
235-
assert "false" == third_log["cold_start"]
236-
assert "false" == fourth_log["cold_start"]
235+
assert third_log["cold_start"] is False
236+
assert fourth_log["cold_start"] is False
237237

238238

239239
def test_log_metric(capsys):

0 commit comments

Comments
 (0)