Juan Hernandez has uploaded a new change for review. Change subject: packaging: Use logging module in engine-service.py ......................................................................
packaging: Use logging module in engine-service.py Currently we use the syslog module directly. This patch replaces that with the logging module and a syslog handler. Change-Id: I34cc1adfb011b26b6969cc65b693b5abed8ae1e5 Signed-off-by: Juan Hernandez <[email protected]> --- M packaging/fedora/engine-service.py.in 1 file changed, 32 insertions(+), 25 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/46/11146/1 diff --git a/packaging/fedora/engine-service.py.in b/packaging/fedora/engine-service.py.in index 278bc25..1b96297 100644 --- a/packaging/fedora/engine-service.py.in +++ b/packaging/fedora/engine-service.py.in @@ -24,6 +24,8 @@ import errno import glob import grp +import logging +import logging.handlers import optparse import os import pwd @@ -33,7 +35,6 @@ import stat import string import sys -import syslog import time import traceback @@ -318,9 +319,8 @@ try: resource.setrlimit(limit, (value, value)) except: - syslog.syslog(syslog.LOG_WARNING, - "Can't change the value of the resource " - "limit \"%s\" to %d." % (name, value)) + logging.warn("Can't change the value of the resource " + "limit \"%s\" to %d.", name, value) def startEngine(): @@ -345,10 +345,8 @@ "enable it again." % (enginePidFile, enginePid)) else: - syslog.syslog(syslog.LOG_WARNING, - "The engine PID file \"%s\" exists and the " - "process %d is running." % - (enginePidFile, enginePid)) + logging.warn("The engine PID file \"%s\" exists and the process " + "%d is running.", enginePidFile, enginePid) return # The list of applications to be deployed: @@ -358,14 +356,16 @@ # Do nothing if the application is not available: engineAppDir = os.path.join(engineUsrDir, engineApp) if not os.path.exists(engineAppDir): - syslog.syslog(syslog.LOG_WARNING, "The application \"%s\" doesn't exist, it will be ignored." % engineAppDir) + logging.warn("The application \"%s\" doesn't exist, it will be " + "ignored.", engineAppDir) continue # Make sure the application is linked in the deployments directory, if not # link it now: engineAppLink = os.path.join(engineDeploymentsDir, engineApp) if not os.path.islink(engineAppLink): - syslog.syslog(syslog.LOG_INFO, "The symbolic link \"%s\" doesn't exist, will create it now." % engineAppLink) + logging.info("The symbolic link \"%s\" doesn't exist, will create " + "it now.", engineAppLink) try: os.symlink(engineAppDir, engineAppLink) except: @@ -501,7 +501,7 @@ # If this is the parent process then the last thing we have to do is # saving the child process PID to the file: if enginePid != 0: - syslog.syslog(syslog.LOG_INFO, "Started engine process %d." % enginePid) + logging.info("Started engine process %d.", enginePid) saveEnginePid(enginePid) return @@ -551,12 +551,14 @@ # Load the PID: enginePid = loadEnginePid() if not enginePid: - syslog.syslog(syslog.LOG_INFO, "The engine PID file \"%s\" doesn't exist." % enginePidFile) + logging.warn("The engine PID file \"%s\" doesn't exist.", enginePidFile) return # First check that the process exists: if not os.path.exists("/proc/%d" % enginePid): - syslog.syslog(syslog.LOG_WARNING, "The engine PID file \"%s\" contains %d, but that process doesn't exist, will just remove the file." % (enginePidFile, enginePid)) + logging.warn("The engine PID file \"%s\" contains %d, but that process " + "doesn't exist, will just remove the file.", + enginePidFile, enginePid) removeEnginePid() return @@ -570,7 +572,8 @@ initialTime = time.time() timeElapsed = 0 while os.path.exists("/proc/%d" % enginePid): - syslog.syslog(syslog.LOG_INFO, "Waiting up to %d seconds for engine process %d to finish." % ((stopTime - timeElapsed), enginePid)) + logging.info("Waiting up to %d seconds for engine process %d to finish.", + stopTime - timeElapsed, enginePid) timeElapsed = time.time() - initialTime if timeElapsed > stopTime: break @@ -579,11 +582,12 @@ # If the process didn't dissapear after the allowed time then we forcibly # kill it: if os.path.exists("/proc/%d" % enginePid): - syslog.syslog(syslog.LOG_WARNING, "The engine process %d didn't finish after waiting %d seconds, killing it." % (enginePid, timeElapsed)) + logging.warn("The engine process %d didn't finish after waiting %d " + "seconds, killing it.", enginePid, timeElapsed) os.kill(enginePid, signal.SIGKILL) - syslog.syslog(syslog.LOG_WARNING, "Killed engine process %d." % enginePid) + logging.warn("Killed engine process %d.", enginePid) else: - syslog.syslog(syslog.LOG_INFO, "Stopped engine process %d." % enginePid) + logging.info("Stopped engine process %d.", enginePid) # Remove the PID file: removeEnginePid() @@ -685,13 +689,21 @@ prettyAction("Stopping", stopEngine) prettyAction("Starting", startEngine) except Exception as exception: - syslog.syslog(syslog.LOG_ERR, str(exception)) + logging.error(str(exception)) sys.exit(1) else: sys.exit(0) def main(): + # Configure logging: + logHandler = logging.handlers.SysLogHandler(address="/dev/log") + logHandler = logging.handlers.SysLogHandler(address="/dev/log") + logFormatter = logging.Formatter(engineName + "[%(process)d]: %(message)s") + logHandler.setFormatter(logFormatter) + logging.root.addHandler(logHandler) + logging.root.setLevel(logging.INFO) + # Check the arguments: args = sys.argv[1:] if len(args) != 1: @@ -702,13 +714,8 @@ showUsage() sys.exit(1) - # Run the action with syslog open and remember to close it - # regardless of what happens in the middle: - syslog.openlog(engineName, syslog.LOG_PID) - try: - performAction(action) - finally: - syslog.closelog() + # Run the action: + performAction(action) if __name__ == "__main__": -- To view, visit http://gerrit.ovirt.org/11146 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I34cc1adfb011b26b6969cc65b693b5abed8ae1e5 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
