Created
          November 22, 2015 10:24 
        
      - 
      
- 
        Save gothma/2459959b58b8af895574 to your computer and use it in GitHub Desktop. 
    Python logging: Trigger exit on critical error
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from __future__ import print_function | |
| import logging | |
| import sys | |
| class ShutdownHandler(logging.Handler): | |
| def emit(self, record): | |
| print(record.msg, file=sys.stderr) | |
| logging.shutdown() | |
| sys.exit(1) | |
| logging.getLogger().addHandler(ShutdownHandler(level=50)) | 
class ShutdownHandler(logging.StreamHandler):
    def emit(self, record):
        super(ExitHandler, self).emit(record)
        if record.levelno >= logging.CRITICAL:
            sys.exit(1)This worked out a bit better for me. Sending level as a param to logging.Handler doesn't work for me but manually checking it in emit with logging.StreamHandler as the base class worked.
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
logging.shutdown()is unnecessary as stated by the logging.shutdown() docs