title: Python: sys.excepthook and logging uncaught exceptions across multiple modules
body: <p>In all of my Python main scripts and modules, I have been trying to implement a way to log uncaught exceptions to the module in which the exception was thrown's logger.  I'm doing this the same way in all of my files:</p>
  
  <pre><code>def log_unhandled_exception(*exc_info):
     text = "".join(traceback.format_exception(*exc_info))
     logger.critical("An unhandled exception has caused this script to terminate prematurely.  Here are the details: {0}".format(text))
     sys.exit(2)
  
  def some_function():
     # ... 
  
  sys.excepthook = log_unhandled_exception
  logger = logging.getLogger("Module1") # or "Module2", "Module3", etc., each module has it's own logger
  </code></pre>
  
  <p>When I run into an uncaught exception, I sometimes don't get the intended logger.  I think it has to do with the ordering that I import the modules: if I import module1 then import module2, and then call a function in module2 and run into an exception, it seems I get module1's logger.  Conversely if I reverse the order of imports (module2 comes before module1), and I attempt the same test (exception thrown in module2), I correctly get module2's logger.  I would have thought the LATER import would take precedence (overwrite the former's sys.excepthook reference), but no.</p>
  
  <p>I was able to solve this problem (I guess...) by giving each logger reference a unique name in each module.  So to amend the code above, THIS pattern works without regard to the order of module imports:</p>
  
  <pre><code>def log_unhandled_exception(*exc_info):
     text = "".join(traceback.format_exception(*exc_info))
     module1_logger.critical("An unhandled exception has caused this script to terminate prematurely.  Here are the details: {0}".format(text))
     sys.exit(2)
  
  def some_function():
     # ... 
  
  sys.excepthook = log_unhandled_exception
  module1_logger = logging.getLogger("Module1") # or "Module2", "Module3", etc., each module has it's own logger
  </code></pre>
  
  <p>Is this the proper way to achieve my desired goal, which is for every module to have it's own logger, and every module to use it's logger to log uncaught exceptions.</p>
  
  <p>(The actual code is more varied, and has a reason for every module to have their own implementation of log_unhandled_exception())</p>