code: def log_to_file(path=None, io=False, level='INFO'):
      
      log = logging.getLogger('')
      if path:
          fp = open(path, 'w', 1)
          mitogen.core.set_cloexec(fp.fileno())
      else:
          fp = sys.stderr
  
      level = os.environ.get('MITOGEN_LOG_LEVEL', level).upper()
      io = level == 'IO'
      if io:
          level = 'DEBUG'
          logging.getLogger('mitogen.io').setLevel(level)
  
      level = getattr(logging, level, logging.INFO)
      log.setLevel(level)
  
      # Prevent accidental duplicate log_to_file() calls from generating
      # duplicate output.
      for handler_ in reversed(log.handlers):
          if getattr(handler_, 'is_mitogen', None):
              log.handlers.remove(handler_)
  
      handler = logging.StreamHandler(fp)
      handler.is_mitogen = True
      handler.formatter = log_get_formatter()
      log.handlers.insert(0, handler)
docs: Install a new :class:`logging.Handler` writing applications logs to the
      filesystem. Useful when debugging slave IO problems.
  
      Parameters to this function may be overridden at runtime using environment
      variables. See :ref:`logging-env-vars`.
  
      :param str path:
          If not :data:`None`, a filesystem path to write logs to. Otherwise,
          logs are written to :data:`sys.stderr`.
  
      :param bool io:
          If :data:`True`, include extremely verbose IO logs in the output.
          Useful for debugging hangs, less useful for debugging application code.
  
      :param str level:
          Name of the :mod:`logging` package constant that is the minimum level
          to log at. Useful levels are ``DEBUG``, ``INFO``, ``WARNING``, and
          ``ERROR``.