@contextlib.contextmanager
def recorded_logs(logger=None, level=logging.DEBUG, override_disabled=True, override_global_level=True):
 if (logger is None):
  logger = logging.getLogger()
 elif (not isinstance(logger, logging.Logger)):
  logger = logging.getLogger(logger)
 handler = RecordingLogHandler()
 old_level = logger.level
 manager_level = logger.manager.disable
 disabled = logger.disabled
 logger.addHandler(handler)
 try:
  if (level is not None):
   logger.setLevel(level)
  if override_disabled:
   logger.disabled = False
  if override_global_level:
   if ((level is None) and (manager_level > old_level)):
    logger.manager.disable = old_level
   elif ((level is not None) and (manager_level > level)):
    logger.manager.disable = level
  (yield handler)
 finally:
  logger.handlers.remove(handler)
  logger.setLevel(old_level)
  logger.disabled = disabled
  logger.manager.disable = manager_level

@ contextlib . contextmanager def recorded_logs ( logger = None , level = logging . DEBUG , override_disabled = True , override_global_level = True ) : if ( logger is None ) : logger = logging . getLogger ( ) elif ( not isinstance ( logger , logging . Logger ) ) : logger = logging . getLogger ( logger ) handler = RecordingLogHandler ( ) old_level = logger . level manager_level = logger . manager . disable disabled = logger . disabled logger . addHandler ( handler ) try : if ( level is not None ) : logger . setLevel ( level ) if override_disabled : logger . disabled = False if override_global_level : if ( ( level is None ) and ( manager_level > old_level ) ) : logger . manager . disable = old_level elif ( ( level is not None ) and ( manager_level > level ) ) : logger . manager . disable = level ( yield handler ) finally : logger . handlers . remove ( handler ) logger . setLevel ( old_level ) logger . disabled = disabled logger . manager . disable = manager_level
context manager for recording log messages .