Skip to content
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

Standardise loggers in python lambda functions #645

Closed
wants to merge 1 commit into from

Conversation

alexiswl
Copy link
Member

@alexiswl alexiswl commented Nov 3, 2024

Resolves #644

@alexiswl alexiswl added the fix label Nov 3, 2024
@alexiswl alexiswl self-assigned this Nov 3, 2024
Copy link
Member

@victorskl victorskl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.INFO,
force=True,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will remove any preconfigured handlers...
Is that what we want?

The accepted answer claims that the default AWS handler "might also add some metadata to the record if available", which sounds like it could be useful?

Happy to accept the change. Longer term we should probably switch to the lambda power tools...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh those docs are actually very handy!

Hmm, yes you might be right, from this below, the logger may in fact set the requestId as well.

Using structured JSON logs with Python
If you select JSON for your function’s log format, Lambda will send logs output by the Python standard logging library to CloudWatch as structured JSON. Each JSON log object contains at least four key value pairs with the following keys:

"timestamp" - the time the log message was generated

"level" - the log level assigned to the message

"message" - the contents of the log message

"requestId" - the unique request ID for the function invocation

We could rearrange from

import logging
logging.basicConfig(
    level=logging.INFO,
    force=True,
    format='%(asctime)s %(message)s'
)
logger = logging.getLogger()

To just

import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

And then we still get the same log levels, just lose the formatting a bit

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that works locally...
I think the issue was that then on the local env there may not be a log handler at all, hence the fallback to the basisConfig if no handler was found.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the issue was that then on the local env there may not be a log handler at all,

Yes, the answer that looks for handlers didn't seem very fun.

Something worth considering is this too - https://docs.powertools.aws.dev/lambda/python/latest/core/logger/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the power tools logging support. That's what I meant earlier.
(but there are also other reasons to use the power tools)

@victorskl
Copy link
Member

Resolve this PR soon too, @alexiswl...
Opt to upgrade to power tools when appropriate, etc.

@alexiswl
Copy link
Member Author

On the backlog, closing PR for now

@alexiswl alexiswl closed this Nov 21, 2024
@alexiswl alexiswl deleted the enhancement/update-all-python-logging branch December 19, 2024 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lambda Python Log Level basicConfig needs special initialisation
3 participants