code: def catching_logs(handler, formatter=None, level=None):
      """Context manager that prepares the whole logging machinery properly."""
      root_logger = logging.getLogger()
  
      if formatter is not None:
          handler.setFormatter(formatter)
      if level is not None:
          handler.setLevel(level)
  
      # Adding the same handler twice would confuse logging system.
      # Just don't do that.
      add_new_handler = handler not in root_logger.handlers
  
      if add_new_handler:
          root_logger.addHandler(handler)
      if level is not None:
          orig_level = root_logger.level
          root_logger.setLevel(min(orig_level, level))
      try:
          yield handler
      finally:
          if level is not None:
              root_logger.setLevel(orig_level)
          if add_new_handler:
              root_logger.removeHandler(handler)
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import logging
  import re
  from contextlib import contextmanager
  import py
  import six
  import pytest
  from _pytest.compat import dummy_context_manager
  from _pytest.config import create_terminal_writer
  from _pytest.pathlib import Path
label_desc: Use image node_id 4 for calling a global function with example usage: catching_logs(handler, formatter, level) without return types