From 774d0bc662c0db82ca9cd3c1b53a98952d5b123c Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Sun, 25 Oct 2020 18:14:22 +0100 Subject: [PATCH] docs: add more info on conditional keys #195 Signed-off-by: heitorlessa --- docs/content/core/logger.mdx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/content/core/logger.mdx b/docs/content/core/logger.mdx index 977ab58599e..0ad17513eed 100644 --- a/docs/content/core/logger.mdx +++ b/docs/content/core/logger.mdx @@ -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. +
Excerpt output in CloudWatch Logs