Revision 82d9caef lib/utils.py

b/lib/utils.py
1269 1269
  return ReadFile(constants.CLUSTER_PASSWORD_FILE)
1270 1270

  
1271 1271

  
1272
def SetupLogging(logfile, debug=False, stderr_logging=False, program=""):
1273
  """Configures the logging module.
1274

  
1275
  """
1276
  fmt = "%(asctime)s: " + program + " "
1277
  if debug:
1278
    fmt += ("pid=%(process)d/%(threadName)s %(levelname)s"
1279
           " %(module)s:%(lineno)s %(message)s")
1280
  else:
1281
    fmt += "pid=%(process)d %(levelname)s %(message)s"
1282
  formatter = logging.Formatter(fmt)
1283

  
1284
  root_logger = logging.getLogger("")
1285
  root_logger.setLevel(logging.NOTSET)
1286

  
1287
  if stderr_logging:
1288
    stderr_handler = logging.StreamHandler()
1289
    stderr_handler.setFormatter(formatter)
1290
    if debug:
1291
      stderr_handler.setLevel(logging.NOTSET)
1292
    else:
1293
      stderr_handler.setLevel(logging.CRITICAL)
1294
    root_logger.addHandler(stderr_handler)
1295

  
1296
  # this can fail, if the logging directories are not setup or we have
1297
  # a permisssion problem; in this case, it's best to log but ignore
1298
  # the error if stderr_logging is True, and if false we re-raise the
1299
  # exception since otherwise we could run but without any logs at all
1300
  try:
1301
    logfile_handler = logging.FileHandler(logfile)
1302
    logfile_handler.setFormatter(formatter)
1303
    if debug:
1304
      logfile_handler.setLevel(logging.DEBUG)
1305
    else:
1306
      logfile_handler.setLevel(logging.INFO)
1307
    root_logger.addHandler(logfile_handler)
1308
  except EnvironmentError, err:
1309
    if stderr_logging:
1310
      logging.exception("Failed to enable logging to file '%s'", logfile)
1311
    else:
1312
      # we need to re-raise the exception
1313
      raise
1314

  
1315

  
1272 1316
def LockedMethod(fn):
1273 1317
  """Synchronized object access decorator.
1274 1318

  

Also available in: Unified diff