Revision de3b8e39

b/lib/utils.py
2067 2067
  return port
2068 2068

  
2069 2069

  
2070
class LogFileHandler(logging.FileHandler):
2071
  """Log handler that doesn't fallback to stderr.
2072

  
2073
  When an error occurs while writing on the logfile, logging.FileHandler tries
2074
  to log on stderr. This doesn't work in ganeti since stderr is redirected to
2075
  the logfile. This class avoids failures reporting errors to /dev/console.
2076

  
2077
  """
2078
  def __init__(self, filename, mode="a", encoding=None):
2079
    """Open the specified file and use it as the stream for logging.
2080

  
2081
    Also open /dev/console to report errors while logging.
2082

  
2083
    """
2084
    logging.FileHandler.__init__(self, filename, mode, encoding)
2085
    self.console = open(constants.DEV_CONSOLE, "a")
2086

  
2087
  def handleError(self, record):
2088
    """Handle errors which occur during an emit() call.
2089

  
2090
    Try to handle errors with FileHandler method, if it fails write to
2091
    /dev/console.
2092

  
2093
    """
2094
    try:
2095
      logging.Filehandler.handleError(self, record)
2096
    except Exception:
2097
      try:
2098
        self.console.write("Cannot log message:\n%s\n" % self.format(record))
2099
      except Exception:
2100
        # Log handler tried everything it could, now just give up
2101
        pass
2102

  
2103

  
2070 2104
def SetupLogging(logfile, debug=0, stderr_logging=False, program="",
2071 2105
                 multithreaded=False, syslog=constants.SYSLOG_USAGE):
2072 2106
  """Configures the logging module.

Also available in: Unified diff