Python Quirks and Tricks

To log a specific module, simply import the underlying package and add a handler. Here is an example for logging the boto3 AWS client to stdout:
import logging
import sys
​
logging_handler = logging.StreamHandler(stream=sys.stdout)
logging.getLogger('botocore').addHandler(logging_handler)
logging.getLogger('botocore').setLevel(logging.DEBUG)
StreamHandler can be swapped with FileHandler to output log events to a file. In addition, the created handler can be added to other specific modules:
import requests
logging.getLogger('urllib3').addHandler(logging_handler)
logging.getLogger('urllib3').setLevel(logging.DEBUG)
​
​
Copy link