Skip to content

Commit

Permalink
fix: append structured logs when injecting lambda context (#86)
Browse files Browse the repository at this point in the history
* fix: append logs when injecting lambda context #85

* docs: update changelog

Signed-off-by: heitorlessa <lessa@amazon.co.uk>

* chore: bump version to 1.0.1

Signed-off-by: heitorlessa <lessa@amazon.co.uk>
  • Loading branch information
heitorlessa committed Jul 5, 2020
1 parent 0a63141 commit 0670e5e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion aws_lambda_powertools/logging/logger.py
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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=[
Expand Down
27 changes: 27 additions & 0 deletions tests/functional/test_logger.py
Expand Up @@ -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

0 comments on commit 0670e5e

Please sign in to comment.