Skip to content

Commit 054f64c

Browse files
authored
Support .info("foo %s", "bar") formatting in logger (#426)
1 parent e5381ae commit 054f64c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

aws_lambda_powertools/logging/formatter.py

+3
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ def _extract_log_message(self, log_record: logging.LogRecord) -> Union[Dict[str,
176176
if isinstance(message, dict):
177177
return message
178178

179+
if log_record.args: # logger.info("foo %s", "bar") requires formatting
180+
return log_record.getMessage()
181+
179182
if isinstance(message, str): # could be a JSON string
180183
try:
181184
message = self.json_deserializer(message)

tests/functional/test_logger_powertools_formatter.py

+13
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,16 @@ def test_logging_various_primitives(stdout, service_name, message):
275275
# THEN it should raise no serialization/deserialization error
276276
logger.info(message)
277277
json.loads(stdout.getvalue())
278+
279+
280+
def test_log_formatting(stdout, service_name):
281+
# GIVEN a logger with default settings
282+
logger = Logger(service=service_name, stream=stdout)
283+
284+
# WHEN logging a message with formatting
285+
logger.info('["foo %s %d %s", null]', "bar", 123, [1, None])
286+
287+
log_dict: dict = json.loads(stdout.getvalue())
288+
289+
# THEN the formatting should be applied (NB. this is valid json, but hasn't be parsed)
290+
assert log_dict["message"] == '["foo bar 123 [1, None]", null]'

0 commit comments

Comments
 (0)