diff --git a/Makefile.am b/Makefile.am index c6a1d8e00e5e70e7e8bd5ef9e5810e3cc1410757..a76de289b4c9bf086c0063f1b2043a5f06ee74c7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -72,7 +72,6 @@ pkgpython_PYTHON = \ lib/jqueue.py \ lib/jstore.py \ lib/locking.py \ - lib/logger.py \ lib/luxi.py \ lib/mcpu.py \ lib/objects.py \ diff --git a/daemons/ganeti-masterd b/daemons/ganeti-masterd index bee97a277e496a5d02cb5c49c6485234bdc8e29d..13fa25d3ad625a8b037a01fdfcf0af2f2d5edff4 100755 --- a/daemons/ganeti-masterd +++ b/daemons/ganeti-masterd @@ -50,7 +50,6 @@ from ganeti import luxi from ganeti import utils from ganeti import errors from ganeti import ssconf -from ganeti import logger from ganeti import workerpool from ganeti import rpc from ganeti import bootstrap @@ -444,8 +443,8 @@ def main(): utils.WritePidFile(constants.MASTERD_PID) - logger.SetupLogging(constants.LOG_MASTERDAEMON, debug=options.debug, - stderr_logging=not options.fork) + utils.SetupLogging(constants.LOG_MASTERDAEMON, debug=options.debug, + stderr_logging=not options.fork) logging.info("ganeti master daemon startup") diff --git a/daemons/ganeti-noded b/daemons/ganeti-noded index 6b73fc5b969173883808cc645be8ef8a22a8b9ce..f881b2a63d2650983431989857a577ef42d1e2ce 100755 --- a/daemons/ganeti-noded +++ b/daemons/ganeti-noded @@ -35,7 +35,6 @@ import signal from optparse import OptionParser from ganeti import backend -from ganeti import logger from ganeti import constants from ganeti import objects from ganeti import errors @@ -688,8 +687,8 @@ def main(): utils.WritePidFile(constants.NODED_PID) try: - logger.SetupLogging(logfile=constants.LOG_NODESERVER, debug=options.debug, - stderr_logging=not options.fork) + utils.SetupLogging(logfile=constants.LOG_NODESERVER, debug=options.debug, + stderr_logging=not options.fork) logging.info("ganeti node daemon startup") # Prepare job queue diff --git a/daemons/ganeti-rapi b/daemons/ganeti-rapi index d11fefd5809d9e1a965118dbee6282c0506753fb..5d3d01b36b69d6460d4603651cdd685ee42b2771 100755 --- a/daemons/ganeti-rapi +++ b/daemons/ganeti-rapi @@ -28,7 +28,6 @@ import sys import os import signal -from ganeti import logger from ganeti import constants from ganeti import errors from ganeti import http @@ -127,7 +126,7 @@ def main(): if options.fork: utils.Daemonize(logfile=constants.LOG_RAPISERVER) - logger.SetupLogging(constants.LOG_RAPISERVER, debug=options.debug, + utils.SetupLogging(constants.LOG_RAPISERVER, debug=options.debug, stderr_logging=not options.fork) utils.WritePidFile(constants.RAPI_PID) diff --git a/daemons/ganeti-watcher b/daemons/ganeti-watcher index ae1f6e54a6741c7a7864c333f681be61d09011a8..351f102292be468b2b53533295d4c306f56ef51f 100755 --- a/daemons/ganeti-watcher +++ b/daemons/ganeti-watcher @@ -38,7 +38,6 @@ from ganeti import constants from ganeti import serializer from ganeti import errors from ganeti import opcodes -from ganeti import logger from ganeti import cli @@ -423,8 +422,8 @@ def main(): options, args = ParseOptions() - logger.SetupLogging(constants.LOG_WATCHER, debug=options.debug, - stderr_logging=options.debug) + utils.SetupLogging(constants.LOG_WATCHER, debug=options.debug, + stderr_logging=options.debug) try: client = cli.GetClient() diff --git a/lib/cli.py b/lib/cli.py index 4de1c297bd5a6077f9411d6dc29ec434ba4aeddf..7832b29025d37c30bd5dedb57cefc7902b04647c 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -31,7 +31,6 @@ import logging from cStringIO import StringIO from ganeti import utils -from ganeti import logger from ganeti import errors from ganeti import constants from ganeti import opcodes @@ -714,8 +713,8 @@ def GenericMain(commands, override=None, aliases=None): for key, val in override.iteritems(): setattr(options, key, val) - logger.SetupLogging(constants.LOG_COMMANDS, debug=options.debug, - stderr_logging=True, program=binary) + utils.SetupLogging(constants.LOG_COMMANDS, debug=options.debug, + stderr_logging=True, program=binary) utils.debug = options.debug diff --git a/lib/logger.py b/lib/logger.py deleted file mode 100644 index 44ea5d9338fd80dff5f301f6f5974e18d818432a..0000000000000000000000000000000000000000 --- a/lib/logger.py +++ /dev/null @@ -1,104 +0,0 @@ -# -# - -# Copyright (C) 2006, 2007 Google Inc. -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -# 02110-1301, USA. - - -"""Logging for Ganeti - -This module abstracts the logging handling away from the rest of the -Ganeti code. It offers some utility functions for easy logging. - -""" - -# pylint: disable-msg=W0603,C0103 - -import sys -import logging - - -def SetupLogging(logfile, debug=False, stderr_logging=False, program=""): - """Configures the logging module. - - """ - fmt = "%(asctime)s: " + program + " " - if debug: - fmt += ("pid=%(process)d/%(threadName)s %(levelname)s" - " %(module)s:%(lineno)s %(message)s") - else: - fmt += "pid=%(process)d %(levelname)s %(message)s" - formatter = logging.Formatter(fmt) - - root_logger = logging.getLogger("") - root_logger.setLevel(logging.NOTSET) - - if stderr_logging: - stderr_handler = logging.StreamHandler() - stderr_handler.setFormatter(formatter) - if debug: - stderr_handler.setLevel(logging.NOTSET) - else: - stderr_handler.setLevel(logging.CRITICAL) - root_logger.addHandler(stderr_handler) - - # this can fail, if the logging directories are not setup or we have - # a permisssion problem; in this case, it's best to log but ignore - # the error if stderr_logging is True, and if false we re-raise the - # exception since otherwise we could run but without any logs at all - try: - logfile_handler = logging.FileHandler(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, err: - if stderr_logging: - logging.exception("Failed to enable logging to file '%s'", logfile) - else: - # we need to re-raise the exception - raise - - -# Backwards compatibility -Error = logging.error -Info = logging.info -Debug = logging.debug - - -def ToStdout(txt): - """Write a message to stdout only, bypassing the logging system - - Parameters: - - txt: the message - - """ - sys.stdout.write(txt + '\n') - sys.stdout.flush() - - -def ToStderr(txt): - """Write a message to stderr only, bypassing the logging system - - Parameters: - - txt: the message - - """ - sys.stderr.write(txt + '\n') - sys.stderr.flush() diff --git a/lib/utils.py b/lib/utils.py index 761dc0f1a6c272fb1dc5e2ae0e5f4438e823875d..cb4e0ce065f02bc460e6b934005960251bd8102b 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -1269,6 +1269,50 @@ def GetNodeDaemonPassword(): return ReadFile(constants.CLUSTER_PASSWORD_FILE) +def SetupLogging(logfile, debug=False, stderr_logging=False, program=""): + """Configures the logging module. + + """ + fmt = "%(asctime)s: " + program + " " + if debug: + fmt += ("pid=%(process)d/%(threadName)s %(levelname)s" + " %(module)s:%(lineno)s %(message)s") + else: + fmt += "pid=%(process)d %(levelname)s %(message)s" + formatter = logging.Formatter(fmt) + + root_logger = logging.getLogger("") + root_logger.setLevel(logging.NOTSET) + + if stderr_logging: + stderr_handler = logging.StreamHandler() + stderr_handler.setFormatter(formatter) + if debug: + stderr_handler.setLevel(logging.NOTSET) + else: + stderr_handler.setLevel(logging.CRITICAL) + root_logger.addHandler(stderr_handler) + + # this can fail, if the logging directories are not setup or we have + # a permisssion problem; in this case, it's best to log but ignore + # the error if stderr_logging is True, and if false we re-raise the + # exception since otherwise we could run but without any logs at all + try: + logfile_handler = logging.FileHandler(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, err: + if stderr_logging: + logging.exception("Failed to enable logging to file '%s'", logfile) + else: + # we need to re-raise the exception + raise + + def LockedMethod(fn): """Synchronized object access decorator. diff --git a/tools/burnin b/tools/burnin index 6e340135936390c43c4110cb4544f3baedc3b73a..295ea7d558efbac02b5e711ae8ae24f18f3fd6b0 100755 --- a/tools/burnin +++ b/tools/burnin @@ -34,7 +34,6 @@ from ganeti import opcodes from ganeti import mcpu from ganeti import constants from ganeti import cli -from ganeti import logger from ganeti import errors from ganeti import utils @@ -63,7 +62,7 @@ class Burner(object): def __init__(self): """Constructor.""" - logger.SetupLogging(constants.LOG_BURNIN, debug=False, stderr_logging=True) + utils.SetupLogging(constants.LOG_BURNIN, debug=False, stderr_logging=True) self._feed_buf = StringIO() self.nodes = [] self.instances = []