Issue
I’m trying to run a script in AWS lambda and I want to output info level logs to the console after the script runs. I’ve tried looking for help from This post on using logs in lambda buy haven’t had any success. I think AWS Cloudwatch is overriding my configuration shown bellow.
import logging
# log configuration
logging.basicConfig(
format='%(levelname)s: %(message)s',
level=logging.INFO,
encoding="utf-8"
)
I want to set the logging level to logging.info. How can I do this? The runtime is python 3.9
Solution
from my understanding, I think one fix would be to add this:
logging.getLogger().setLevel('INFO')
I believe that logging.basicConfig(level=...)
affects the minimum log level at which logs show up in the console, but across all loggers. The one above explicitly sets the minimum enabled level for the root logger, i.e. logging.getLogger()
. The enabled level for each logger determines at what level messages will be logged – otherwise, each call to a logger method like logging.info
is basically a no-op.
So essentially, the basicConfig
and setLevel
calls are separate, but they work together to determine if a library’s logs are printed to the console. For example, you can set basicConfig(level='DEBUG')
so that the debug-level logs for all libraries should get printed out. But if you want to make an exception for one library like botocore
for example, you can use logging.getLogger('botocore').setLevel('WARNING')
, and that will set the minimum enabled level for that library to WARNING, so only messages logged by this library above that minimum level get printed out to the console.
Answered By – rv.kvetch
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0