[PATCH 4/4] add logging to lorax

Brian C. Lane bcl at redhat.com
Fri Jul 27 20:50:44 UTC 2012


From: "Brian C. Lane" <bcl at redhat.com>

---
 src/sbin/lorax |   39 ++++++++++++++++++++++++++++++++++++++-
 1 files changed, 38 insertions(+), 1 deletions(-)

diff --git a/src/sbin/lorax b/src/sbin/lorax
index b4acd01..c1f709a 100755
--- a/src/sbin/lorax
+++ b/src/sbin/lorax
@@ -20,9 +20,14 @@
 #
 # Red Hat Author(s):  Martin Gracik <mgracik at redhat.com>
 #
-
 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())
 
-- 
1.7.7.6



More information about the anaconda-patches mailing list