From 700d5007a376fbdd06f2374d5ec65e3dbbf0fde6 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Fri, 27 Jul 2012 09:04:01 -0700 Subject: [PATCH] add logging to lorax --- src/sbin/lorax | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/sbin/lorax b/src/sbin/lorax index b4acd01e..1754e958 100755 --- a/src/sbin/lorax +++ b/src/sbin/lorax @@ -20,9 +20,14 @@ # # Red Hat Author(s): Martin Gracik # - from __future__ import print_function +import logging +log = logging.getLogger("lorax") +program_log = logging.getLogger("program") +pylorax_log = logging.getLogger("pylorax") + + import sys import os import tempfile @@ -32,6 +37,32 @@ import ConfigParser import yum import pylorax +def setup_logging(opts): + # Setup logging to console and to logfile + log.setLevel(logging.DEBUG) + pylorax_log.setLevel(logging.DEBUG) + + sh = logging.StreamHandler() + sh.setLevel(logging.INFO) + fmt = logging.Formatter("%(asctime)s: %(message)s") + sh.setFormatter(fmt) + log.addHandler(sh) + pylorax_log.addHandler(sh) + + fh = logging.FileHandler(filename=opts.logfile, mode="w") + fh.setLevel(logging.DEBUG) + fmt = logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s") + fh.setFormatter(fmt) + log.addHandler(fh) + pylorax_log.addHandler(fh) + + # External program output log + program_log.setLevel(logging.DEBUG) + logfile = os.path.abspath(os.path.dirname(opts.logfile))+"/program.log" + fh = logging.FileHandler(filename=logfile, mode="w") + fh.setLevel(logging.DEBUG) + program_log.addHandler(fh) + def main(args): version = "{0} 0.1".format(os.path.basename(args[0])) @@ -76,6 +107,8 @@ def main(args): help="volume id", metavar="STRING") optional.add_option("--nomacboot", help="", action="store_false", default=True, dest="domacboot") + optional.add_option("--logfile", default="./lorax.log", + help="Path to logfile") # add the option groups to the parser parser.add_option_group(required) @@ -105,6 +138,10 @@ def main(args): if os.path.exists(outputdir): parser.error("output directory should not exist.") + opts.logfile = os.path.abspath(opts.logfile) + + setup_logging(opts) + # create the temporary directory for lorax tempdir = tempfile.mkdtemp(prefix="lorax.", dir=tempfile.gettempdir())