def error_trapping(ident=None):
    ''' A context manager that traps and logs exception in its block.
        Usage:
        with error_trapping('optional description'):
            might_raise_exception()
        this_will_always_be_called()
    '''
    try:
        yield None
    except Exception:
        if ident:
            logging.error('Error in ' + ident, exc_info=True)
        else:
            logging.error('Error', exc_info=True)
 A context manager that traps and logs exception in its block.
        Usage:
        with error_trapping('optional description'):
            might_raise_exception()
        this_will_always_be_called()
    
recipes/Python/531821_Error_logging_context_manager/recipe-531821.py