Skip to content

Commit 774d0bc

Browse files
committed
docs: add more info on conditional keys #195
Signed-off-by: heitorlessa <[email protected]>
1 parent 828fec4 commit 774d0bc

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Diff for: docs/content/core/logger.mdx

+9-3
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,18 @@ from aws_lambda_powertools import Logger
158158
logger = Logger()
159159

160160
def handler(event, context):
161-
if "order_id" in event:
162-
logger.structure_logs(append=True, order_id=event["order_id"]) # highlight-line
163-
logger.info("Collecting payment")
161+
order_id = event.get("order_id")
162+
logger.structure_logs(append=True, order_id=order_id) # highlight-line
163+
logger.info("Collecting payment")
164164
...
165165
```
166166

167+
> Note: Logger will automatically reject any key with a None value.
168+
169+
If you conditionally add keys depending on the payload, you can use the highlighted line above as an example.
170+
171+
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.
172+
167173
<details>
168174
<summary><strong>Excerpt output in CloudWatch Logs</strong></summary>
169175

0 commit comments

Comments
 (0)