Skip to content

Commit f07091c

Browse files
Adding documentation
1 parent 6abe3a3 commit f07091c

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

docs/core/logger.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ Logger is commonly initialized in the global scope. Due to [Lambda Execution Con
274274
--8<-- "examples/logger/src/clear_state_event_two.json"
275275
```
276276

277+
### Accessing currently configured keys
278+
279+
You can view all currently configured keys from the Logger state using the `get_current_keys()` method. This method is useful when you need to avoid overwriting keys that are already configured.
280+
281+
=== "get_current_keys.py"
282+
283+
```python hl_lines="4 11"
284+
--8<-- "examples/logger/src/get_current_keys.py"
285+
```
286+
277287
### Log levels
278288

279289
The default log level is `INFO`. It can be set using the `level` constructor option, `setLevel()` method or by using the `POWERTOOLS_LOG_LEVEL` environment variable.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from aws_lambda_powertools import Logger
2+
from aws_lambda_powertools.utilities.typing import LambdaContext
3+
4+
logger = Logger()
5+
6+
7+
@logger.inject_lambda_context
8+
def lambda_handler(event: dict, context: LambdaContext) -> str:
9+
logger.info("Collecting payment")
10+
11+
if "order" not in logger.get_current_keys():
12+
logger.append_keys(order=event.get("order"))
13+
14+
return "hello world"

tests/functional/test_logger.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,11 +614,12 @@ def test_logger_append_and_show_current_keys(stdout, service_name):
614614
logger.append_keys(**extra_keys)
615615

616616
# THEN appended keys must be present in logger
617-
assert "request_id" in logger.get_current_keys()
618-
assert "context" in logger.get_current_keys()
617+
current_keys = logger.get_current_keys()
618+
assert "request_id" in current_keys
619+
assert "context" in current_keys
619620

620621

621-
def test_logger_formatter_without_current_keys_method(stdout, service_name):
622+
def test_logger_formatter_without_get_current_keys_method(stdout, service_name):
622623
class CustomFormatter(BasePowertoolsFormatter):
623624
def append_keys(self, **additional_keys):
624625
# Fake method

0 commit comments

Comments
 (0)