Closing python protocols and exiting the application

I am using the logger in an application, and it occurred to me that it would be neat if the logger supported a method that gracefully closes file descriptors, etc., and then exits the application.

For example:

logger = logging.getLogger('my_app')
logger.fatal("We're toast!")

      

a fatal method (or some such) would then be:

  • register the message as usual
  • logging.shutdown ()
  • Call sys.exit (1)

Thoughts? Does something like this exist? It is a bad idea?

Why do I need it? Well, there are a few places in my code where I want the application to die and it seems like wasting time repeating the code to do 2 and 3 .

+2


source to share


1 answer


This may not be the cleanest solution, but this comes to mind:

try:
    # Your main function
    main()
except:
    logger.exception('some message')
    sys.exit(1)

      

And in valid code, just raise any exception



Although this will give you a different registrar. When it comes to the completion part, just use try / finally:

try:
    # do whatever here
    main()
finally:
    logging.shutdown()

      

0


source







All Articles