How Do You Capture Error Messages in a File of Your Choice Using Python Rather Than Have Them Printed to the Screen?

Problem scenario
You have a Python program that displays urllib3 (or some other type) of message to the screen.  You do not want them echoed there cluttering the screen.  You want to redirect the output to a log file (to permanently store the data).  How do you get a Python program to write to a log file?

Solution
Use these three lines, but replace "contint" with the name of your choice:

import logging

logging.basicConfig(filename='contint.log', filemode='a', level=logging.DEBUG)
logging.captureWarnings(True)

Leave a comment

Your email address will not be published. Required fields are marked *