-
Notifications
You must be signed in to change notification settings - Fork 408
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
Feature request: Include name of standard library loggers #1267
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
hey @kbakk thank you for flagging this, and we appreciate even more for finding that You can include the logger name by ensuring your Powertools Logger has that attribute, this will ensure any registered standard library logger that receive a copy of your Powertools Logger config also has it. Here's an example: app.py import logging
from aws_lambda_powertools import Logger
from aws_lambda_powertools.logging import utils
a_random_logger = logging.getLogger(__name__) # could've been any other registered library logger, etc.
# NOTE: we're including `name` key which will be propagated to our Powertools Formatter
# and knowing it's a reserved log attribute it'll resolve for each log message accordingly
# https://docs.python.org/3/library/logging.html#logrecord-attributes
powertools_logger = Logger(service="payment", name="%(name)s")
utils.copy_config_to_registered_loggers(source_logger=powertools_logger, include={a_random_logger.name})
powertools_logger.info("Name should be equal service value")
a_random_logger.info("Something random") # name will be __main__ because it's a single file/module sample output [
{
"level": "INFO",
"location": "<module>:16",
"message": "Name should be equal service value",
"timestamp": "2022-06-29 16:56:11,789+0200",
"service": "payment",
"name": "payment"
},
{
"level": "INFO",
"location": "<module>:17",
"message": "Something random",
"timestamp": "2022-06-29 16:56:11,789+0200",
"service": "payment",
"name": "__main__"
}
] The reason we didn't do this before-hand was largely cost, because we couldn't predict whether that was going to be useful for customers - introducing a new key would mean an additional log ingestion/storage in CloudWatch Logs. That being said, we could definitely make this easier by adjusting Let me know if this helps or whether I misunderstood your issue. If it is helpful, we should surely add it in the docs ;) Thank you! |
Thank you, that did the trick! Is this behavior documented somewhere (I couldn't find it mentioned here https://awslabs.github.io/aws-lambda-powertools-python/latest/api/logging/index.html) or would I be using an undocumented feature? Should I leave this open until it's documented? |
Awesome! Let's improve the docs by creating a dedicated section to make it more explicit - it's briefly documented here (note I can work on the docs tomorrow unless you'd like to try making that your first contribution with our guidance ;) Fast forward after the pause (end of July), we should revisit it and implement a new field |
Side-note: Reason I asked was that the |
Sure, I'll try! I'll do a PR where I add a section following https://awslabs.github.io/aws-lambda-powertools-python/latest/core/logger/#overriding-log-records Maybe it should be mentioned what |
Perfect - thank you <3!! We can figure out the best naming and such afterwards. We can start by making sure everyone is aware all of the standard logging attributes can be added at any point - Logger & Powertools Formatter constructor, and https://docs.python.org/3/library/logging.html#logrecord-attributes |
PR created 🙂 |
PR reviewed ;) We can merge as soon as those suggestions are added/amended. Thank you!!! |
Merged. We'll revisit the addition of the Thank you one more time @kbakk and congrats on your first contrib!! |
Adding |
@leandrodamascena wanna take this? |
Looking into adding the |
Done, pending review #1568 In the next release, when you use This ensures it's an opt-in feature and we don't include [
{
"level": "INFO",
"location": "<module>:16",
"message": "Name should be equal service value",
"timestamp": "2022-06-29 16:56:11,789+0200",
"service": "payment",
"name": "payment"
},
{
"level": "INFO",
"location": "<module>:17",
"message": "Something random",
"timestamp": "2022-06-29 16:56:11,789+0200",
"service": "payment",
"name": "my_dependency_logger"
}
] |
Sounds like a good improvement, thanks for adding this! 😀 |
|
This is now released under 1.30.0 version! |
Use case
In order to locate the source of a log message, the name of the standard library logger often gives a hint of where it's coming from, as it's a convention to use
logging.getLogger(__name__)
to name the logger (see https://docs.python.org/3/howto/logging-cookbook.html#using-logging-in-multiple-modules for an example).When following the FAQ guide step "How can I enable powertools logging for imported libraries? (permalink)"
(btw,
logging.logger()
needs to be changed tologger.getLogger("my.precious.logger")
)I will get a log message of
I am missing the name of the logger. In the above case it's not that difficult to locate where the log originated from, but take this example:
It's difficult to see what library emitted that log message.
Solution/User Experience
Ideally, when aws-lambda-powertools-python is logging from standard library loggers, it should include the logger name as well.
Alternative solutions
Acknowledgment
The text was updated successfully, but these errors were encountered: