@contextmanager
def patch_logger(logger_name, log_level, log_kwargs=False):
 calls = []
 def replacement(msg, *args, **kwargs):
  call = (msg % args)
  calls.append(((call, kwargs) if log_kwargs else call))
 logger = logging.getLogger(logger_name)
 orig = getattr(logger, log_level)
 setattr(logger, log_level, replacement)
 try:
  (yield calls)
 finally:
  setattr(logger, log_level, orig)

@ contextmanager def patch_logger ( logger_name , log_level , log_kwargs = False ) : calls = [ ] def replacement ( msg , * args , ** kwargs ) : call = ( msg % args ) calls . append ( ( ( call , kwargs ) if log_kwargs else call ) ) logger = logging . getLogger ( logger_name ) orig = getattr ( logger , log_level ) setattr ( logger , log_level , replacement ) try : ( yield calls ) finally : setattr ( logger , log_level , orig )
context manager that takes a named logger and the logging level and provides a simple mock-like list of messages received .