Fix bug related to log opening failures
authorIustin Pop <iustin@google.com>
Thu, 10 Mar 2011 11:19:17 +0000 (12:19 +0100)
committerIustin Pop <iustin@google.com>
Thu, 10 Mar 2011 14:29:44 +0000 (15:29 +0100)
If opening the log file fails, then we shouldn't attempt to use that
variable.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>

lib/utils/log.py

index f02c823..5100800 100644 (file)
@@ -245,9 +245,18 @@ def SetupLogging(logfile, program, debug=0, stderr_logging=False,
     # exception since otherwise we could run but without any logs at all
     try:
       if console_logging:
     # exception since otherwise we could run but without any logs at all
     try:
       if console_logging:
-        logfile_handler = _LogHandler(open(constants.DEV_CONSOLE, "a"), logfile)
+        logfile_handler = _LogHandler(open(constants.DEV_CONSOLE, "a"),
+                                      logfile)
       else:
         logfile_handler = _ReopenableLogHandler(logfile)
       else:
         logfile_handler = _ReopenableLogHandler(logfile)
+
+      logfile_handler.setFormatter(formatter)
+      if debug:
+        logfile_handler.setLevel(logging.DEBUG)
+      else:
+        logfile_handler.setLevel(logging.INFO)
+      root_logger.addHandler(logfile_handler)
+      reopen_handlers.append(logfile_handler)
     except EnvironmentError:
       if stderr_logging or syslog == constants.SYSLOG_YES:
         logging.exception("Failed to enable logging to file '%s'", logfile)
     except EnvironmentError:
       if stderr_logging or syslog == constants.SYSLOG_YES:
         logging.exception("Failed to enable logging to file '%s'", logfile)
@@ -255,13 +264,4 @@ def SetupLogging(logfile, program, debug=0, stderr_logging=False,
         # we need to re-raise the exception
         raise
 
         # we need to re-raise the exception
         raise
 
-    logfile_handler.setFormatter(formatter)
-    if debug:
-      logfile_handler.setLevel(logging.DEBUG)
-    else:
-      logfile_handler.setLevel(logging.INFO)
-    root_logger.addHandler(logfile_handler)
-
-    reopen_handlers.append(logfile_handler)
-
   return compat.partial(_ReopenLogFiles, reopen_handlers)
   return compat.partial(_ReopenLogFiles, reopen_handlers)