From 0a6314188ccb42f680c956958a04863cf6968314 Mon Sep 17 00:00:00 2001 From: Heitor Lessa Date: Fri, 3 Jul 2020 14:34:36 +0100 Subject: [PATCH 1/2] docs: add blog post in the readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 18de8e00e0d..0d8164d172e 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A suite of utilities for AWS Lambda Functions that makes tracing with AWS X-Ray, structured logging and creating custom metrics asynchronously easier. -**[📜Documentation](https://awslabs.github.io/aws-lambda-powertools-python/)** | **[API Docs](https://awslabs.github.io/aws-lambda-powertools-python/api/)** | **[🐍PyPi](https://pypi.org/project/aws-lambda-powertools/)** | **[Feature request](https://github.com/awslabs/aws-lambda-powertools-python/issues/new?assignees=&labels=feature-request%2C+triage&template=feature_request.md&title=)** | **[🐛Bug Report](https://github.com/awslabs/aws-lambda-powertools-python/issues/new?assignees=&labels=bug%2C+triage&template=bug_report.md&title=)** | **[Kitchen sink example](https://github.com/awslabs/aws-lambda-powertools-python/tree/develop/example)** +**[📜Documentation](https://awslabs.github.io/aws-lambda-powertools-python/)** | **[API Docs](https://awslabs.github.io/aws-lambda-powertools-python/api/)** | **[🐍PyPi](https://pypi.org/project/aws-lambda-powertools/)** | **[Feature request](https://github.com/awslabs/aws-lambda-powertools-python/issues/new?assignees=&labels=feature-request%2C+triage&template=feature_request.md&title=)** | **[🐛Bug Report](https://github.com/awslabs/aws-lambda-powertools-python/issues/new?assignees=&labels=bug%2C+triage&template=bug_report.md&title=)** | **[Kitchen sink example](https://github.com/awslabs/aws-lambda-powertools-python/tree/develop/example)** | **[Detailed blog post](https://aws.amazon.com/blogs/opensource/simplifying-serverless-best-practices-with-lambda-powertools/)** ## Features From 0670e5efa09b60f0dd6d040a1a3b37225c94b516 Mon Sep 17 00:00:00 2001 From: Heitor Lessa Date: Sun, 5 Jul 2020 20:26:20 +0100 Subject: [PATCH 2/2] fix: append structured logs when injecting lambda context (#86) * fix: append logs when injecting lambda context #85 * docs: update changelog Signed-off-by: heitorlessa * chore: bump version to 1.0.1 Signed-off-by: heitorlessa --- CHANGELOG.md | 4 ++++ aws_lambda_powertools/logging/logger.py | 2 +- pyproject.toml | 2 +- tests/functional/test_logger.py | 27 +++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c924e2a8f48..51269fdee7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.0.1] - 2020-07-06 +### Fixed +- **Logger**: Fix a bug with `inject_lambda_context` causing existing an Logger keys to be overriden if `structure_logs` was called before + ## [1.0.0] - 2020-06-18 ### Added - **Metrics**: `add_metadata` method to add any metric metadata you'd like to ease finding metric related data via CloudWatch Logs diff --git a/aws_lambda_powertools/logging/logger.py b/aws_lambda_powertools/logging/logger.py index 9a943536b4e..101cbc6ac54 100644 --- a/aws_lambda_powertools/logging/logger.py +++ b/aws_lambda_powertools/logging/logger.py @@ -197,7 +197,7 @@ def decorate(event, context): lambda_context = build_lambda_context_model(context) cold_start = _is_cold_start() - self.structure_logs(cold_start=cold_start, **lambda_context.__dict__) + self.structure_logs(append=True, cold_start=cold_start, **lambda_context.__dict__) return lambda_handler(event, context) return decorate diff --git a/pyproject.toml b/pyproject.toml index 79fc7753f07..40035fa3fbf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "aws_lambda_powertools" -version = "1.0.0" +version = "1.0.1" description = "Python utilities for AWS Lambda functions including but not limited to tracing, logging and custom metric" authors = ["Amazon Web Services"] classifiers=[ diff --git a/tests/functional/test_logger.py b/tests/functional/test_logger.py index 211f12f8fc1..e402a3d0f2a 100644 --- a/tests/functional/test_logger.py +++ b/tests/functional/test_logger.py @@ -256,3 +256,30 @@ def test_logger_invalid_sampling_rate(): # THEN we should raise InvalidLoggerSamplingRateError with pytest.raises(InvalidLoggerSamplingRateError): Logger(sampling_rate="TEST") + + +def test_inject_lambda_context_with_structured_log(lambda_context, stdout): + # GIVEN Logger is initialized + logger = Logger(stream=stdout) + + # WHEN structure_logs has been used to add an additional key upfront + # and a lambda function is decorated with logger.inject_lambda_context + logger.structure_logs(append=True, additional_key="test") + + @logger.inject_lambda_context + def handler(event, context): + logger.info("Hello") + + handler({}, lambda_context) + + # THEN lambda contextual info should always be in the logs + log = capture_logging_output(stdout) + expected_logger_context_keys = ( + "function_name", + "function_memory_size", + "function_arn", + "function_request_id", + "additional_key", + ) + for key in expected_logger_context_keys: + assert key in log