hand made class with destructor and the logging module

C

climb65

Hello,

I have a handmade class within a python program which uses the logging
module.
This class has a destructor which is expected to log something into my log
file if a crash occurs.
Well, the destructor works well but during the crash, python has already
closed the log file and the reference to the Logger has been set to None.
So, I can't log anything!

How is it possible to solve this problem?

Thanks.
 
V

Vinay Sajip

This class has a destructor which is expected to log something into my log
file if a crash occurs.
Well, the destructor works well but during the crash, python has already
closed the log file and the reference to the Logger has been set to None.
So, I can't log anything!

How is it possible to solve this problem?

Unfortunately, when a process is about to exit (for whatever reason),
things may not shut down in an order you would like. I'd advise
wrapping your main program in an try: except: block. If you have a
main, function, for example:

import logging, sys

logger = logging.getLogger(__name__)

def main():
#configure logging, say using basicConfig()
try:
# here is whatever your main program is doing now
retcode = 0
except Exception:
logger.exception('Exception raised during processing')
retcode = 1
sys.exit(retcode)

if __name__ == '__main__':
main()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top