From cfcc79c664ab1926770399ad760703abb3a8b9bb Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Mon, 31 Jan 2011 17:03:18 +0100 Subject: [PATCH] utils.SetupLogging: Make program a mandatory argument MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's passed in by most users (daemons, CLI scripts) and for the others (burnin, watcher) it certainly doesn't hurt, especially when using syslog. Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: RenΓ© Nussbaumer <rn@google.com> --- lib/cli.py | 4 ++-- lib/daemon.py | 3 +-- lib/utils/log.py | 13 ++++++++----- lib/watcher/__init__.py | 5 +++-- tools/burnin | 3 ++- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/cli.py b/lib/cli.py index ce97e998a..e9576db0e 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -1913,8 +1913,8 @@ def GenericMain(commands, override=None, aliases=None): for key, val in override.iteritems(): setattr(options, key, val) - utils.SetupLogging(constants.LOG_COMMANDS, debug=options.debug, - stderr_logging=True, program=binary) + utils.SetupLogging(constants.LOG_COMMANDS, binary, debug=options.debug, + stderr_logging=True) if old_cmdline: logging.info("run with arguments '%s'", old_cmdline) diff --git a/lib/daemon.py b/lib/daemon.py index c26a17625..6fb2f6995 100644 --- a/lib/daemon.py +++ b/lib/daemon.py @@ -659,11 +659,10 @@ def GenericMain(daemon_name, optionparser, utils.WritePidFile(utils.DaemonPidFileName(daemon_name)) try: try: - utils.SetupLogging(logfile=constants.DAEMONS_LOGFILES[daemon_name], + utils.SetupLogging(constants.DAEMONS_LOGFILES[daemon_name], daemon_name, debug=options.debug, stderr_logging=not options.fork, multithreaded=multithreaded, - program=daemon_name, syslog=options.syslog, console_logging=console_logging) if callable(prepare_fn): diff --git a/lib/utils/log.py b/lib/utils/log.py index f91d44a86..361bd985c 100644 --- a/lib/utils/log.py +++ b/lib/utils/log.py @@ -22,6 +22,7 @@ """ +import os.path import logging import logging.handlers @@ -162,20 +163,20 @@ def _GetLogFormatter(program, multithreaded, debug, syslog): return logging.Formatter("".join(parts)) -def SetupLogging(logfile, debug=0, stderr_logging=False, program="", +def SetupLogging(logfile, program, debug=0, stderr_logging=False, multithreaded=False, syslog=constants.SYSLOG_USAGE, console_logging=False): """Configures the logging module. @type logfile: str @param logfile: the filename to which we should log + @type program: str + @param program: the name under which we should log messages @type debug: integer @param debug: if greater than zero, enable debug messages, otherwise only those at C{INFO} and above level @type stderr_logging: boolean @param stderr_logging: whether we should also log to the standard error - @type program: str - @param program: the name under which we should log messages @type multithreaded: boolean @param multithreaded: if True, will add the thread name to the log file @type syslog: string @@ -190,8 +191,10 @@ def SetupLogging(logfile, debug=0, stderr_logging=False, program="", syslog/stderr logging is disabled """ - formatter = _GetLogFormatter(program, multithreaded, debug, False) - syslog_fmt = _GetLogFormatter(program, multithreaded, debug, True) + progname = os.path.basename(program) + + formatter = _GetLogFormatter(progname, multithreaded, debug, False) + syslog_fmt = _GetLogFormatter(progname, multithreaded, debug, True) root_logger = logging.getLogger("") root_logger.setLevel(logging.NOTSET) diff --git a/lib/watcher/__init__.py b/lib/watcher/__init__.py index 4503c63ae..098f8c2d5 100644 --- a/lib/watcher/__init__.py +++ b/lib/watcher/__init__.py @@ -32,6 +32,7 @@ by a node reboot. Run from cron or similar. # C0103: Invalid name ganeti-watcher import os +import os.path import sys import time import logging @@ -714,8 +715,8 @@ def Main(): print >> sys.stderr, ("Usage: %s [-f] " % sys.argv[0]) return constants.EXIT_FAILURE - utils.SetupLogging(constants.LOG_WATCHER, debug=options.debug, - stderr_logging=options.debug) + utils.SetupLogging(constants.LOG_WATCHER, sys.argv[0], + debug=options.debug, stderr_logging=options.debug) if ShouldPause() and not options.ignore_pause: logging.debug("Pause has been set, exiting") diff --git a/tools/burnin b/tools/burnin index b1b0d2bb1..a47ea19b4 100755 --- a/tools/burnin +++ b/tools/burnin @@ -1071,7 +1071,8 @@ def main(): """Main function. """ - utils.SetupLogging(constants.LOG_BURNIN, debug=False, stderr_logging=True) + utils.SetupLogging(constants.LOG_BURNIN, sys.argv[0], + debug=False, stderr_logging=True) return Burner().BurninCluster() -- GitLab