Skip to content

Commit 0670e5e

Browse files
authored
fix: append structured logs when injecting lambda context (#86)
* fix: append logs when injecting lambda context #85 * docs: update changelog Signed-off-by: heitorlessa <[email protected]> * chore: bump version to 1.0.1 Signed-off-by: heitorlessa <[email protected]>
1 parent 0a63141 commit 0670e5e

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.0.1] - 2020-07-06
10+
### Fixed
11+
- **Logger**: Fix a bug with `inject_lambda_context` causing existing an Logger keys to be overriden if `structure_logs` was called before
12+
913
## [1.0.0] - 2020-06-18
1014
### Added
1115
- **Metrics**: `add_metadata` method to add any metric metadata you'd like to ease finding metric related data via CloudWatch Logs

Diff for: aws_lambda_powertools/logging/logger.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def decorate(event, context):
197197
lambda_context = build_lambda_context_model(context)
198198
cold_start = _is_cold_start()
199199

200-
self.structure_logs(cold_start=cold_start, **lambda_context.__dict__)
200+
self.structure_logs(append=True, cold_start=cold_start, **lambda_context.__dict__)
201201
return lambda_handler(event, context)
202202

203203
return decorate

Diff for: pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "aws_lambda_powertools"
3-
version = "1.0.0"
3+
version = "1.0.1"
44
description = "Python utilities for AWS Lambda functions including but not limited to tracing, logging and custom metric"
55
authors = ["Amazon Web Services"]
66
classifiers=[

Diff for: tests/functional/test_logger.py

+27
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,30 @@ def test_logger_invalid_sampling_rate():
256256
# THEN we should raise InvalidLoggerSamplingRateError
257257
with pytest.raises(InvalidLoggerSamplingRateError):
258258
Logger(sampling_rate="TEST")
259+
260+
261+
def test_inject_lambda_context_with_structured_log(lambda_context, stdout):
262+
# GIVEN Logger is initialized
263+
logger = Logger(stream=stdout)
264+
265+
# WHEN structure_logs has been used to add an additional key upfront
266+
# and a lambda function is decorated with logger.inject_lambda_context
267+
logger.structure_logs(append=True, additional_key="test")
268+
269+
@logger.inject_lambda_context
270+
def handler(event, context):
271+
logger.info("Hello")
272+
273+
handler({}, lambda_context)
274+
275+
# THEN lambda contextual info should always be in the logs
276+
log = capture_logging_output(stdout)
277+
expected_logger_context_keys = (
278+
"function_name",
279+
"function_memory_size",
280+
"function_arn",
281+
"function_request_id",
282+
"additional_key",
283+
)
284+
for key in expected_logger_context_keys:
285+
assert key in log

0 commit comments

Comments
 (0)