Enable Powertools logging for imported libraries #799
Replies: 2 comments 7 replies
-
hey Fredrik, thanks for opening an issue.
Besides boto, are there any other existing loggers you want to change? If
you think it's helpful, we could add an additional method to make this
process easier - can't think of a name now but it's doable, then you would
simply have one line to do this and we'll safely do that for you.
The reason you're seeing duplicates or more is that Lambda inadvertently
<https://docs.aws.amazon.com/lambda/latest/dg/python-logging.html#python-logging-lib>,
so when you set *pyLogging.getLogger().setLevel(pyLogging.INFO)* it allows
any log messages from any logger to be propagated due to Python Logging
hierarchy.
…On Thu, 4 Nov 2021 at 11:19, Fredrik Meyer ***@***.***> wrote:
Until recently, I used the following code for enabling Powertools logging
for imported libraries:
from aws_lambda_powertools import Loggerimport osimport logging as pyLogging
# https://stackoverflow.com/a/56579088/1013553pyLogging.getLogger().setLevel(pyLogging.INFO)
import boto3
boto3.set_stream_logger()boto3.set_stream_logger("botocore")
location_format = "%(name)s.%(funcName)s:%(lineno)d"
logger = Logger(service=os.environ["POWERTOOLS_SERVICE_NAME"], location=location_format)
# Enable JSON logging for librariesloggers = [pyLogging.getLogger(name) for name in pyLogging.root.manager.loggerDict]for l in loggers:
l.addHandler(logger.registered_handler)
This worked, but resulted in duplicated logs (sometimes three times).
Is there a recommended way to make all logs JSON and with the same level?
Thanks!
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#799>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAZPQBCJSMXKXTMRMZLY26LUKJMZVANCNFSM5HLCHIYA>
.
|
Beta Was this translation helpful? Give feedback.
6 replies
-
I was thinking of a public method for this in the Logger class, so it’s
more explicit on the behaviour this drives - and we can have docstrings to
tell them what this does in case the name isn’t perfect for the job.
Hardest part is naming :-)
*As a public method:*
logger = Logger()
logger.clone_config_to_all_loggers(level=…, deny_list=[“logger_named_X”,
“botocore”])
*As a standalone function*
logger = Logger()
clone_logger_handler(source_logger=logger, deny_list=[“logger_named_X”,
“botocore”])
I like the latter as it’s standalone, since we’re not modifying anything at
the source logger but copying its Handler & Log Level.
But naming is hard haha
…On Thu, 4 Nov 2021 at 12:52, Fredrik Meyer ***@***.***> wrote:
Thank for the very quick reply!
I'll make a feature request straight away. I'm not familiar with the logic
of the log handlers, but if it's very easy I could try to do a PR as well.
So you're thinking something like this?
logger = Logger(...., level="INFO", deny_list=["dont-log-this-library", "and-not-this"])
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#799 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAZPQBBBDTPKSDNEYGBZ653UKJXWNANCNFSM5HLCHIYA>
.
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
heitorlessa
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Until recently, I used the following code for enabling Powertools logging for imported libraries:
This worked, but resulted in duplicated logs (sometimes three times).
Is there a recommended way to make all logs JSON and with the same level?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions