I have a python script running in background, and I want to log all the exception and output to a log file.
I know to use logging module and try.. catch.. to log exception, but what if I missed some, is there any way to log these exceptions too?
I have a python script running in background, and I want to log all the exception and output to a log file.
I know to use logging module and try.. catch.. to log exception, but what if I missed some, is there any way to log these exceptions too?
You can reassign sys.stdout and sys.stderr to file handles. They'll be available after your python exits if an uncaught exception is encountered. Something like this:
import sys
sys.stdout = open('myOut.txt', 'w')
sys.stderr = open('myErr.txt', 'w')
logging module supports the write(str) method, which means you could assign an instance of a log to either or both of these.