Skip to content

docs: add more info on conditional keys #195 #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions docs/content/core/logger.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,18 @@ from aws_lambda_powertools import Logger
logger = Logger()

def handler(event, context):
if "order_id" in event:
logger.structure_logs(append=True, order_id=event["order_id"]) # highlight-line
logger.info("Collecting payment")
order_id = event.get("order_id")
logger.structure_logs(append=True, order_id=order_id) # highlight-line
logger.info("Collecting payment")
...
```

> Note: Logger will automatically reject any key with a None value.

If you conditionally add keys depending on the payload, you can use the highlighted line above as an example.

This example will add `order_id` if its value is not empty, and in subsequent invocations where `order_id` might not be present it'll remove it from the logger.

<details>
<summary><strong>Excerpt output in CloudWatch Logs</strong></summary>

Expand Down