From ff5fac045a18b6e79f9ba490728a54d5c73dd666 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Wed, 9 Jul 2008 14:46:16 +0000
Subject: [PATCH] Fix double-logging in daemons

Currently, in debug mode, both the logfile handler and the stderr
handler will log debug messages. Since the stderr is redirected to the
same logfile (to catch non-logged errors), it means log entries are
doubled.

The patch adds an extra parameter to the logger.SetupDaemon() function
that allows disabling of the stderr logging. The master and node daemon
will use this to enable stderr logging only when running in foreground.

Reviewed-by: imsnah
---
 daemons/ganeti-masterd | 3 ++-
 daemons/ganeti-noded   | 3 ++-
 lib/logger.py          | 7 ++++---
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/daemons/ganeti-masterd b/daemons/ganeti-masterd
index 5c8d11692..8e3701c17 100755
--- a/daemons/ganeti-masterd
+++ b/daemons/ganeti-masterd
@@ -391,7 +391,8 @@ def main():
     utils.Daemonize(logfile=constants.LOG_MASTERDAEMON,
                     noclose_fds=[master.fileno()])
 
-  logger.SetupDaemon(constants.LOG_MASTERDAEMON, debug=options.debug)
+  logger.SetupDaemon(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 b5d198416..cb72b4cf3 100755
--- a/daemons/ganeti-noded
+++ b/daemons/ganeti-noded
@@ -633,7 +633,8 @@ def main():
   if options.fork:
     utils.Daemonize(logfile=constants.LOG_NODESERVER)
 
-  logger.SetupDaemon(logfile=constants.LOG_NODESERVER, debug=options.debug)
+  logger.SetupDaemon(logfile=constants.LOG_NODESERVER, debug=options.debug,
+                     stderr_logging=not options.fork)
   logging.info("ganeti node daemon startup")
 
   global _EXIT_GANETI_NODED
diff --git a/lib/logger.py b/lib/logger.py
index f92dbaa7f..37573ee47 100644
--- a/lib/logger.py
+++ b/lib/logger.py
@@ -84,12 +84,12 @@ def SetupLogging(program='ganeti', debug=False):
   root_logger.addHandler(stderr_file)
 
 
-def SetupDaemon(logfile, debug=False):
+def SetupDaemon(logfile, debug=False, stderr_logging=False):
   """Configures the logging module for daemons
 
   """
   if debug:
-    fmt = "%(asctime)s: %(levelname)s %(pathname)s:%(lineno)s %(message)s"
+    fmt = "%(asctime)s: %(levelname)s %(module)s:%(lineno)s %(message)s"
   else:
     fmt = "%(asctime)s: %(levelname)s %(message)s"
   formatter = logging.Formatter(fmt)
@@ -109,7 +109,8 @@ def SetupDaemon(logfile, debug=False):
   root_logger = logging.getLogger("")
   root_logger.setLevel(logging.NOTSET)
   root_logger.addHandler(logfile_handler)
-  root_logger.addHandler(stderr_handler)
+  if stderr_logging:
+    root_logger.addHandler(stderr_handler)
 
 
 # Backwards compatibility
-- 
GitLab