Skip to content

Commit 80101eb

Browse files
Merge branch 'develop' into dependabot/docker/docs/squidfunk/mkdocs-material-471695f3e611d9858788ac04e4daa9af961ccab73f1c0f545e90f8cc5d4268b8
2 parents 340c8f3 + 62f856b commit 80101eb

24 files changed

+160
-33
lines changed

.github/workflows/bootstrap_region.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
steps:
4646
- id: credentials
4747
name: AWS Credentials
48-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
48+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
4949
with:
5050
aws-region: ${{ inputs.region }}
5151
role-to-assume: ${{ secrets.REGION_IAM_ROLE }}
@@ -91,14 +91,14 @@ jobs:
9191
steps:
9292
- id: credentials
9393
name: AWS Credentials
94-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
94+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
9595
with:
9696
aws-region: us-east-1
9797
role-to-assume: ${{ secrets.REGION_IAM_ROLE }}
9898
mask-aws-account-id: true
9999
- id: go-setup
100100
name: Setup Go
101-
uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
101+
uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # v5.3.0
102102
- id: go-env
103103
name: Go Env
104104
run: go env

.github/workflows/dispatch_analytics.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
statuses: read
4444
steps:
4545
- name: Configure AWS credentials
46-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
46+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
4747
with:
4848
aws-region: eu-central-1
4949
role-to-assume: ${{ secrets.AWS_LAYERS_ROLE_ARN }}

.github/workflows/layer_govcloud.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
environment: Prod (Readonly)
6060
steps:
6161
- name: Configure AWS Credentials
62-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
62+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
6363
with:
6464
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
6565
aws-region: us-east-1
@@ -116,7 +116,7 @@ jobs:
116116
SHA=$(jq -r '.Content.CodeSha256' '${{ matrix.layer }}_${{ matrix.arch }}.json')
117117
test "$(openssl dgst -sha256 -binary ${{ matrix.layer }}_${{ matrix.arch }}.zip | openssl enc -base64)" == "$SHA" && echo "SHA OK: ${SHA}" || exit 1
118118
- name: Configure AWS Credentials
119-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
119+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
120120
with:
121121
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
122122
aws-region: us-gov-east-1
@@ -185,7 +185,7 @@ jobs:
185185
SHA=$(jq -r '.Content.CodeSha256' '${{ matrix.layer }}_${{ matrix.arch }}.json')
186186
test "$(openssl dgst -sha256 -binary ${{ matrix.layer }}_${{ matrix.arch }}.zip | openssl enc -base64)" == "$SHA" && echo "SHA OK: ${SHA}" || exit 1
187187
- name: Configure AWS Credentials
188-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
188+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
189189
with:
190190
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
191191
aws-region: us-gov-west-1

.github/workflows/layer_govcloud_python313.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
environment: Prod (Readonly)
5656
steps:
5757
- name: Configure AWS Credentials
58-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
58+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
5959
with:
6060
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
6161
aws-region: us-east-1
@@ -108,7 +108,7 @@ jobs:
108108
SHA=$(jq -r '.Content.CodeSha256' '${{ matrix.layer }}_${{ matrix.arch }}.json')
109109
test "$(openssl dgst -sha256 -binary ${{ matrix.layer }}_${{ matrix.arch }}.zip | openssl enc -base64)" == "$SHA" && echo "SHA OK: ${SHA}" || exit 1
110110
- name: Configure AWS Credentials
111-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
111+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
112112
with:
113113
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
114114
aws-region: us-gov-east-1
@@ -173,7 +173,7 @@ jobs:
173173
SHA=$(jq -r '.Content.CodeSha256' '${{ matrix.layer }}_${{ matrix.arch }}.json')
174174
test "$(openssl dgst -sha256 -binary ${{ matrix.layer }}_${{ matrix.arch }}.zip | openssl enc -base64)" == "$SHA" && echo "SHA OK: ${SHA}" || exit 1
175175
- name: Configure AWS Credentials
176-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
176+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
177177
with:
178178
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
179179
aws-region: us-gov-west-1

.github/workflows/layer_govcloud_verify.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
environment: Prod (Readonly)
4040
steps:
4141
- name: Configure AWS Credentials
42-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
42+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
4343
with:
4444
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
4545
aws-region: us-east-1
@@ -69,7 +69,7 @@ jobs:
6969
environment: GovCloud Prod (East)
7070
steps:
7171
- name: Configure AWS Credentials
72-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
72+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
7373
with:
7474
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
7575
aws-region: us-gov-east-1
@@ -100,7 +100,7 @@ jobs:
100100
environment: GovCloud Prod (West)
101101
steps:
102102
- name: Configure AWS Credentials
103-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
103+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
104104
with:
105105
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
106106
aws-region: us-gov-east-1

.github/workflows/layer_rename.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
environment: layer-prod
5757
steps:
5858
- name: Configure AWS Credentials
59-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
59+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
6060
with:
6161
role-to-assume: ${{ secrets.AWS_LAYERS_ROLE_ARN }}
6262
aws-region: us-east-1
@@ -140,7 +140,7 @@ jobs:
140140
SHA=$(jq -r '.Content.CodeSha256' ${{ matrix.layer }}_x86_64.json)
141141
test $(openssl dgst -sha256 -binary ${{ matrix.layer }}_x86_64.zip | openssl enc -base64) == $SHA && echo "SHA OK: ${SHA}" || exit 1
142142
- name: Configure AWS Credentials
143-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
143+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
144144
with:
145145
role-to-assume: ${{ secrets.AWS_LAYERS_ROLE_ARN }}
146146
aws-region: ${{ matrix.region }}

.github/workflows/pre-release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ jobs:
232232

233233
- name: Upload to PyPi prod
234234
if: ${{ !inputs.skip_pypi }}
235-
uses: pypa/gh-action-pypi-publish@67339c736fd9354cd4f8cb0b744f2b82a74b5c70 # v1.12.3
235+
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
236236

237237
# Creates a PR with the latest version we've just released
238238
# since our trunk is protected against any direct pushes from automation

.github/workflows/release-drafter.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ jobs:
2727
permissions:
2828
contents: write # create release in draft mode
2929
steps:
30-
- uses: release-drafter/release-drafter@3f0f87098bd6b5c5b9a36d49c41d998ea58f9348 # v5.20.1
30+
- uses: release-drafter/release-drafter@b1476f6e6eb133afa41ed8589daba6dc69b4d3f5 # v5.20.1
3131
env:
3232
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/release-v3.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,12 @@ jobs:
237237

238238
- name: Upload to PyPi prod
239239
if: ${{ !inputs.skip_pypi }}
240-
uses: pypa/gh-action-pypi-publish@67339c736fd9354cd4f8cb0b744f2b82a74b5c70 # v1.12.3
240+
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
241241

242242
# PyPi test maintenance affected us numerous times, leaving for history purposes
243243
# - name: Upload to PyPi test
244244
# if: ${{ !inputs.skip_pypi }}
245-
# uses: pypa/gh-action-pypi-publish@67339c736fd9354cd4f8cb0b744f2b82a74b5c70 # v1.12.3
245+
# uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
246246
# with:
247247
# repository-url: https://test.pypi.org/legacy/
248248

.github/workflows/release.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,12 @@ jobs:
237237

238238
- name: Upload to PyPi prod
239239
if: ${{ !inputs.skip_pypi }}
240-
uses: pypa/gh-action-pypi-publish@67339c736fd9354cd4f8cb0b744f2b82a74b5c70 # v1.12.3
240+
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
241241

242242
# PyPi test maintenance affected us numerous times, leaving for history purposes
243243
# - name: Upload to PyPi test
244244
# if: ${{ !inputs.skip_pypi }}
245-
# uses: pypa/gh-action-pypi-publish@67339c736fd9354cd4f8cb0b744f2b82a74b5c70 # v1.12.3
245+
# uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
246246
# with:
247247
# repository-url: https://test.pypi.org/legacy/
248248

.github/workflows/reusable_deploy_v2_layer_stack.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ jobs:
153153
- name: Install poetry
154154
run: pipx install git+https://github.com/python-poetry/poetry@bd500dd3bdfaec3de6894144c9cedb3a9358be84 # v2.0.1
155155
- name: Configure AWS Credentials
156-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
156+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
157157
with:
158158
aws-region: ${{ matrix.region }}
159159
role-to-assume: ${{ secrets.AWS_LAYERS_ROLE_ARN }}

.github/workflows/reusable_deploy_v2_sar.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ jobs:
9090
artifact_name: ${{ inputs.source_code_artifact_name }}
9191

9292
- name: Configure AWS credentials
93-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
93+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
9494
with:
9595
aws-region: ${{ env.AWS_REGION }}
9696
role-to-assume: ${{ secrets.AWS_LAYERS_ROLE_ARN }}
@@ -101,7 +101,7 @@ jobs:
101101
# we then jump to our specific SAR Account with the correctly scoped IAM Role
102102
# this allows us to have a single trail when a release occurs for a given layer (beta+prod+SAR beta+SAR prod)
103103
- name: AWS credentials SAR role
104-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
104+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
105105
id: aws-credentials-sar-role
106106
with:
107107
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}

.github/workflows/reusable_deploy_v3_layer_stack.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ jobs:
157157
pipx install git+https://github.com/python-poetry/poetry@bd500dd3bdfaec3de6894144c9cedb3a9358be84 # v2.0.1
158158
pipx inject poetry git+https://github.com/python-poetry/poetry-plugin-export@8c83d26603ca94f2e203bfded7b6d7f530960e06 # v1.8.0
159159
- name: Configure AWS credentials
160-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
160+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
161161
with:
162162
aws-region: ${{ matrix.region }}
163163
role-to-assume: ${{ secrets.AWS_LAYERS_ROLE_ARN }}

.github/workflows/reusable_deploy_v3_sar.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ jobs:
8787

8888

8989
- name: Configure AWS credentials
90-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
90+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
9191
with:
9292
aws-region: ${{ env.AWS_REGION }}
9393
role-to-assume: ${{ secrets.AWS_LAYERS_ROLE_ARN }}
@@ -98,7 +98,7 @@ jobs:
9898
# we then jump to our specific SAR Account with the correctly scoped IAM Role
9999
# this allows us to have a single trail when a release occurs for a given layer (beta+prod+SAR beta+SAR prod)
100100
- name: AWS credentials SAR role
101-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
101+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
102102
id: aws-credentials-sar-role
103103
with:
104104
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}

.github/workflows/reusable_publish_docs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ jobs:
7979
poetry run mike set-default --push latest
8080
8181
- name: Configure AWS credentials
82-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
82+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
8383
with:
8484
aws-region: us-east-1
8585
role-to-assume: ${{ secrets.AWS_DOCS_ROLE_ARN }}

.github/workflows/run-e2e-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272
- name: Install dependencies
7373
run: make dev-quality-code
7474
- name: Configure AWS credentials
75-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
75+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
7676
with:
7777
role-to-assume: ${{ secrets.AWS_TEST_ROLE_ARN }}
7878
aws-region: ${{ env.AWS_DEFAULT_REGION }}

.github/workflows/secure_workflows.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
- name: Checkout code
3333
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
3434
- name: Ensure 3rd party workflows have SHA pinned
35-
uses: zgosalvez/github-actions-ensure-sha-pinned-actions@64418826697dcd77c93a8e4a1f7601a1942e57b5 # v3.0.18
35+
uses: zgosalvez/github-actions-ensure-sha-pinned-actions@c3a2b64f69b7a1542a68f44d9edbd9ec3fc1455e # v3.0.20
3636
with:
3737
allowlist: |
3838
slsa-framework/slsa-github-generator

.github/workflows/update_ssm.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ jobs:
6666
run: |
6767
echo 'CONVERTED_REGION=${{ matrix.region }}' | tr 'a-z\-' 'A-Z_' >> "$GITHUB_OUTPUT"
6868
- id: creds
69-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
69+
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
7070
with:
7171
aws-region: ${{ matrix.region }}
7272
role-to-assume: ${{ secrets[format('{0}', steps.transform.outputs.CONVERTED_REGION)] }}

aws_lambda_powertools/logging/logger.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def __init__(
222222
choice=sampling_rate,
223223
env=os.getenv(constants.LOGGER_LOG_SAMPLING_RATE),
224224
)
225+
self._default_log_keys: dict[str, Any] = {"service": self.service, "sampling_rate": self.sampling_rate}
225226
self.child = child
226227
self.logger_formatter = logger_formatter
227228
self._stream = stream or sys.stdout
@@ -231,7 +232,6 @@ def __init__(
231232
self._is_deduplication_disabled = resolve_truthy_env_var_choice(
232233
env=os.getenv(constants.LOGGER_LOG_DEDUPLICATION_ENV, "false"),
233234
)
234-
self._default_log_keys = {"service": self.service, "sampling_rate": self.sampling_rate}
235235
self._logger = self._get_logger()
236236

237237
# NOTE: This is primarily to improve UX, so IDEs can autocomplete LambdaPowertoolsFormatter options
@@ -605,6 +605,14 @@ def append_context_keys(self, **additional_keys: Any) -> Generator[None, None, N
605605
with self.registered_formatter.append_context_keys(**additional_keys):
606606
yield
607607

608+
def clear_state(self) -> None:
609+
"""Removes all custom keys that were appended to the Logger."""
610+
# Clear all custom keys from the formatter
611+
self.registered_formatter.clear_state()
612+
613+
# Reset to default keys
614+
self.structure_logs(**self._default_log_keys)
615+
608616
# These specific thread-safe methods are necessary to manage shared context in concurrent environments.
609617
# They prevent race conditions and ensure data consistency across multiple threads.
610618
def thread_safe_append_keys(self, **additional_keys: object) -> None:

docs/core/logger.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,15 @@ You can remove any additional key from Logger state using `remove_keys`.
274274

275275
#### Clearing all state
276276

277+
##### Decorator with clear_state
278+
277279
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){target="_blank"}, 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.
278280

279281
???+ tip "Tip: When is this useful?"
280282
It 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.
281283

282284
???+ danger "Danger: This can have unintended side effects if you use Layers"
283-
Lambda Layers code is imported before the Lambda handler.
285+
Lambda Layers code is imported before the Lambda handler. When a Lambda function starts, it first imports and executes all code in the Layers (including any global scope code) before proceeding to the function's own code.
284286

285287
This means that `clear_state=True` will instruct Logger to remove any keys previously added before Lambda handler execution proceeds.
286288

@@ -304,6 +306,27 @@ Logger is commonly initialized in the global scope. Due to [Lambda Execution Con
304306
--8<-- "examples/logger/src/clear_state_event_two.json"
305307
```
306308

309+
##### clear_state method
310+
311+
You can call `clear_state()` as a method explicitly within your code to clear appended keys at any point during the execution of your Lambda invocation.
312+
313+
=== "clear_state_method.py"
314+
315+
```python hl_lines="12"
316+
--8<-- "examples/logger/src/clear_state_method.py"
317+
```
318+
=== "Output before clear_state()"
319+
320+
```json hl_lines="9 17"
321+
--8<-- "examples/logger/src/before_clear_state.json"
322+
```
323+
324+
=== "Output after clear_state()"
325+
326+
```json hl_lines="4"
327+
--8<-- "examples/logger/src/after_clear_state.json"
328+
```
329+
307330
### Accessing currently configured keys
308331

309332
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"level": "INFO",
3+
"location": "lambda_handler:126",
4+
"message": "State after clearing - only show default keys",
5+
"timestamp": "2025-01-30 13:56:03,158-0300",
6+
"service": "payment"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"logs": [
3+
{
4+
"level": "INFO",
5+
"location": "lambda_handler:122",
6+
"message": "Starting order processing",
7+
"timestamp": "2025-01-30 13:56:03,157-0300",
8+
"service": "payment",
9+
"order_id": "12345"
10+
},
11+
{
12+
"level": "INFO",
13+
"location": "lambda_handler:124",
14+
"message": "Final state before clearing",
15+
"timestamp": "2025-01-30 13:56:03,157-0300",
16+
"service": "payment",
17+
"order_id": "12345"
18+
}
19+
]
20+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from aws_lambda_powertools import Logger
2+
from aws_lambda_powertools.utilities.typing import LambdaContext
3+
4+
logger = Logger(service="payment", level="DEBUG")
5+
6+
7+
def lambda_handler(event: dict, context: LambdaContext) -> str:
8+
try:
9+
logger.append_keys(order_id="12345")
10+
logger.info("Starting order processing")
11+
finally:
12+
logger.info("Final state before clearing")
13+
logger.clear_state()
14+
logger.info("State after clearing - only show default keys")
15+
return "Completed"

0 commit comments

Comments
 (0)