Skip to content

Commit db6870d

Browse files
author
Michael Brewer
committed
feat(logging): add get_correlation_id
1 parent 902f8c3 commit db6870d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

aws_lambda_powertools/logging/logger.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,16 @@ def set_correlation_id(self, value: Optional[str]):
400400
else:
401401
self.append_keys(correlation_id=value)
402402

403+
def get_correlation_id(self) -> Optional[str]:
404+
"""Gets the correlation_id in the logging json
405+
406+
Returns
407+
-------
408+
str, optional
409+
Value for the correlation id
410+
"""
411+
return self.registered_formatter.log_format.get("correlation_id")
412+
403413
@staticmethod
404414
def _get_log_level(level: Union[str, int, None]) -> Union[str, int]:
405415
"""Returns preferred log level set by the customer in upper case"""

tests/functional/test_logger.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,18 @@ def test_logger_set_correlation_id_to_none(lambda_context, stdout, service_name)
474474
assert "correlation_id" not in log
475475

476476

477+
def test_logger_get_correlation_id(lambda_context, stdout, service_name):
478+
# GIVEN a logger with a correlation_id set
479+
logger = Logger(service=service_name, stream=stdout)
480+
logger.set_correlation_id("foo")
481+
482+
# WHEN calling get_correlation_id
483+
correlation_id = logger.get_correlation_id()
484+
485+
# THEN it should return the correlation_id
486+
assert "foo" == correlation_id
487+
488+
477489
def test_logger_set_correlation_id_path(lambda_context, stdout, service_name):
478490
# GIVEN
479491
logger = Logger(service=service_name, stream=stdout)

0 commit comments

Comments
 (0)