-
Notifications
You must be signed in to change notification settings - Fork 421
Clarify logger.structure_logs behavior #195
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
Comments
Hey @ivamluz - Thank you for raising a high quality issue, and for using Powertools :) Background. Your assumption is correct, as it also applies for anything outside the handler as it can be persisted across invocations when the execution context (Lambda container/process) is reused. Recommended solution. You could solve your use case with good defaults by always including both keys. Any key added to the logger with an empty value will be discarded - Here's your last snippet updated. from aws_lambda_powertools import Logger
logger = Logger(child=True)
def handler(event, context):
# It'll default to None if these keys don't exist in event
order_id = event.get("order_id")
other_param = event.get("other_param")
# If order_id or other_param values are None they will be ignored
# If such key already exists its value will be now overriden with None, and hence dropped from log keys
logger.structure_logs(append=True, order_id=order_id, other_param=other_param) Let us know if that makes sense - If it does, I'll mark this to update the docs to clarify this use case. Thank you / Obrigado ;) |
Thanks for your support, @heitorlessa . I'll give it a try soon. |
Signed-off-by: heitorlessa <[email protected]>
docs: add more info on conditional keys #195
hey @ivamluz, We're preparing for 1.7.0 release tomorrow with a new parser, and I've just merged a documentation fix to address your original question- Here's a screenshot of how it'll look like tomorrow Thank you (once again) for reporting it :) |
Hey @ivamluz - I'm gonna close this as I can reproduce it, and docs have been updated as part of the 1.7.0 release with some exciting news too Full release details: https://github.com/awslabs/aws-lambda-powertools-python/releases/tag/v1.7.0 |
Hey, @heitorlessa , sorry for the delay in getting back to you. I just tested your solution and it works. Thanks again for your support and congrats to the team for the great library. Just love how easy it is to implement custom middlewares with the help of it :) |
What were you initially searching for in the docs?
Understand if logger.structure_logs affects the behavior of all requests or only the current one.
The documentation here shows an example of an order_id being appended to the logs:
Looking at the source code for the
structure_logs
method, I can see the existing formatters are going to be updated with the new arguments:If I have a parent Logger object being initialized inside my package init.py like this:
Does it mean that the logger will be initialized once (in the cold start) and every call to
logger.structure_logs
will update the existing formatters, causing logging calls from different requests to be mixed up? For example, if I have something like this in my handler:And I receive two requests:
Questions
Does it mean the second request will also have the order_id included in the logs (because it was set by the first request, and the existing formatter was updated), even if this second one is expected not to have it logged?
If 2the two requests have the order_id parameter, and they are different, what is the expected behavior.
If the description above correctly describes the logging behavior, and this is by design, is there a recommended way to add structured log keys conditionally on a per-request basis?
Thanks in advance for any help you can provide on this matter.
The text was updated successfully, but these errors were encountered: