Skip to content

Commit d1b7c22

Browse files
committed
refactor: rename to clear_state
1 parent 9019f30 commit d1b7c22

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

aws_lambda_powertools/logging/logger.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,13 @@ def inject_lambda_context(
264264
lambda_handler: Callable[[Dict, Any], Any] = None,
265265
log_event: bool = None,
266266
correlation_id_path: str = None,
267-
remove_custom_keys: bool = False,
267+
clear_state: bool = False,
268268
):
269269
"""Decorator to capture Lambda contextual info and inject into logger
270270
271271
Parameters
272272
----------
273-
remove_custom_keys : bool, optional
273+
clear_state : bool, optional
274274
Instructs logger to remove any custom keys previously added
275275
lambda_handler : Callable
276276
Method to inject the lambda context
@@ -320,7 +320,7 @@ def handler(event, context):
320320
self.inject_lambda_context,
321321
log_event=log_event,
322322
correlation_id_path=correlation_id_path,
323-
remove_custom_keys=remove_custom_keys,
323+
clear_state=clear_state,
324324
)
325325

326326
log_event = resolve_truthy_env_var_choice(
@@ -332,7 +332,7 @@ def decorate(event, context):
332332
lambda_context = build_lambda_context_model(context)
333333
cold_start = _is_cold_start()
334334

335-
if remove_custom_keys:
335+
if clear_state:
336336
self.structure_logs(cold_start=cold_start, **lambda_context.__dict__)
337337
else:
338338
self.append_keys(cold_start=cold_start, **lambda_context.__dict__)

docs/core/logger.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ We provide [built-in JMESPath expressions](#built-in-correlation-id-expressions)
232232
### Appending additional keys
233233

234234
!!! info "Custom keys are persisted across warm invocations"
235-
Always set additional keys as part of your handler to ensure they have the latest value, or explicitly clear them with `remove_custom_keys=True`as explained above.
235+
Always set additional keys as part of your handler to ensure they have the latest value, or explicitly clear them with `clear_state=True`as explained above.
236236

237237

238238
You can append additional keys using either mechanism:
@@ -427,17 +427,17 @@ You can remove any additional key from Logger state using `remove_keys`.
427427
}
428428
```
429429

430-
#### Removing all custom keys
430+
#### Clearing all state
431431

432-
Logger is commonly initialized in the global scope. Due to [Lambda Execution Context reuse](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html), this means that custom keys can be persisted across invocations. If you want all custom keys to be deleted, you can use `remove_custom_keys=True` param in `inject_lambda_context` decorator.
432+
Logger is commonly initialized in the global scope. Due to [Lambda Execution Context reuse](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html), this means that custom keys can be persisted across invocations. If you want all custom keys to be deleted, you can use `clear_state=True` param in `inject_lambda_context` decorator.
433433

434434
!!! info
435435
This is useful when you add multiple custom keys conditionally, instead of setting a default `None` value if not present. Any key with `None` value is automatically removed by Logger.
436436

437437
!!! danger "This can have unintended side effects if you use Layers"
438438
Lambda Layers code is imported before the Lambda handler.
439439

440-
This means that `remove_custom_keys=True` will instruct Logger to remove any keys previously added before Lambda handler execution proceeds.
440+
This means that `clear_state=True` will instruct Logger to remove any keys previously added before Lambda handler execution proceeds.
441441

442442
You can either avoid running any code as part of Lambda Layers global scope, or override keys with their latest value as part of handler's execution.
443443

@@ -448,7 +448,7 @@ Logger is commonly initialized in the global scope. Due to [Lambda Execution Con
448448

449449
logger = Logger(service="payment")
450450

451-
@logger.inject_lambda_context(remove_custom_keys=True)
451+
@logger.inject_lambda_context(clear_state=True)
452452
def handler(event, context):
453453
if event.get("special_key"):
454454
# Should only be available in the first request log

tests/functional/test_logger.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,12 +564,12 @@ def handler(event, context):
564564
assert "custom handler" in log
565565

566566

567-
def test_clear_keys_on_inject_lambda_context(lambda_context, stdout, service_name):
567+
def test_clear_state_on_inject_lambda_context(lambda_context, stdout, service_name):
568568
# GIVEN
569569
logger = Logger(service=service_name, stream=stdout)
570570

571-
# WHEN remove_custom_keys is set and a key was conditionally added in the first invocation
572-
@logger.inject_lambda_context(remove_custom_keys=True)
571+
# WHEN clear_state is set and a key was conditionally added in the first invocation
572+
@logger.inject_lambda_context(clear_state=True)
573573
def handler(event, context):
574574
if event.get("add_key"):
575575
logger.append_keys(my_key="value")

0 commit comments

Comments
 (0)