utils.log: Restrict I/O error handling coverage
authorMichael Hanselmann <hansmi@google.com>
Mon, 31 Jan 2011 15:46:21 +0000 (16:46 +0100)
committerMichael Hanselmann <hansmi@google.com>
Wed, 2 Feb 2011 15:57:28 +0000 (16:57 +0100)
The I/O error will occur while opening the file, not while adding
and configuring the handler.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: RenĂ© Nussbaumer <rn@google.com>

lib/utils/log.py

index 1bb2c9f..f91d44a 100644 (file)
@@ -229,16 +229,16 @@ def SetupLogging(logfile, debug=0, stderr_logging=False, program="",
         logfile_handler = _LogHandler(open(constants.DEV_CONSOLE, "a"), 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)
     except EnvironmentError:
       if stderr_logging or syslog == constants.SYSLOG_YES:
         logging.exception("Failed to enable logging to file '%s'", logfile)
       else:
         # 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)