Skip to content

Commit 87a227c

Browse files
Merge pull request #125 from gridsmartercities/log_func_name
added function name to all messages output by log decorator
2 parents 9912cef + e9f55e6 commit 87a227c

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

aws_lambda_decorators/decorators.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
LOGGER = get_logger(__name__)
1515

1616

17-
PARAM_LOG_MESSAGE = "Parameters: %s"
18-
RESPONSE_LOG_MESSAGE = "Response: %s"
17+
PARAM_LOG_MESSAGE = "Function: %s, Parameters: %s"
18+
RESPONSE_LOG_MESSAGE = "Function: %s, Response: %s"
1919
EXCEPTION_LOG_MESSAGE = "%s: %s in argument %s for path %s"
2020
EXCEPTION_LOG_MESSAGE_PATHLESS = "%s: %s in argument %s"
2121
ERROR_MESSAGE = "Error extracting parameters"
@@ -164,10 +164,10 @@ def log(parameters=False, response=False):
164164
def decorator(func):
165165
def wrapper(*args, **kwargs):
166166
if parameters:
167-
LOGGER.info(PARAM_LOG_MESSAGE, args)
167+
LOGGER.info(PARAM_LOG_MESSAGE, func.__name__, args)
168168
func_response = func(*args, **kwargs)
169169
if response:
170-
LOGGER.info(RESPONSE_LOG_MESSAGE, func_response)
170+
LOGGER.info(RESPONSE_LOG_MESSAGE, func.__name__, func_response)
171171
return func_response
172172
return wrapper
173173
return decorator

examples/test_examples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ def test_log_example(mock_logger):
192192

193193
# and check the log messages were produced.
194194
mock_logger.info.assert_has_calls([
195-
call("Parameters: %s", ("Hello!",)),
196-
call("Response: %s", "Done!")
195+
call("Function: %s, Parameters: %s", "log_example", ("Hello!",)),
196+
call("Function: %s, Response: %s", "log_example", "Done!")
197197
])
198198

199199
@patch("boto3.resource")

tests/test_decorators.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ def handler(event, context, an_other): # noqa
484484

485485
handler("first", "{\"tests\": \"a\"}", "another")
486486

487-
mock_logger.info.assert_called_once_with("Parameters: %s", ("first", "{\"tests\": \"a\"}", "another"))
487+
mock_logger.info.assert_called_once_with(
488+
"Function: %s, Parameters: %s", "handler", ("first", "{\"tests\": \"a\"}", "another"))
488489

489490
@patch("aws_lambda_decorators.decorators.LOGGER")
490491
def test_log_decorator_can_log_response(self, mock_logger): # noqa: pylint - no-self-use
@@ -495,7 +496,7 @@ def handler():
495496

496497
handler()
497498

498-
mock_logger.info.assert_called_once_with("Response: %s", {"statusCode": 201})
499+
mock_logger.info.assert_called_once_with("Function: %s, Response: %s", "handler", {"statusCode": 201})
499500

500501
@patch("boto3.client")
501502
def test_get_valid_ssm_parameter(self, mock_boto_client):

0 commit comments

Comments
 (0)