add logging to lorax
This commit is contained in:
		
							parent
							
								
									505d5bf164
								
							
						
					
					
						commit
						700d5007a3
					
				| @ -20,9 +20,14 @@ | |||||||
| # | # | ||||||
| # Red Hat Author(s):  Martin Gracik <mgracik@redhat.com> | # Red Hat Author(s):  Martin Gracik <mgracik@redhat.com> | ||||||
| # | # | ||||||
| 
 |  | ||||||
| from __future__ import print_function | from __future__ import print_function | ||||||
| 
 | 
 | ||||||
|  | import logging | ||||||
|  | log = logging.getLogger("lorax") | ||||||
|  | program_log = logging.getLogger("program") | ||||||
|  | pylorax_log = logging.getLogger("pylorax") | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| import sys | import sys | ||||||
| import os | import os | ||||||
| import tempfile | import tempfile | ||||||
| @ -32,6 +37,32 @@ import ConfigParser | |||||||
| import yum | import yum | ||||||
| import pylorax | 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): | def main(args): | ||||||
|     version = "{0} 0.1".format(os.path.basename(args[0])) |     version = "{0} 0.1".format(os.path.basename(args[0])) | ||||||
| @ -76,6 +107,8 @@ def main(args): | |||||||
|             help="volume id", metavar="STRING") |             help="volume id", metavar="STRING") | ||||||
|     optional.add_option("--nomacboot", help="", |     optional.add_option("--nomacboot", help="", | ||||||
|             action="store_false", default=True, dest="domacboot") |             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 |     # add the option groups to the parser | ||||||
|     parser.add_option_group(required) |     parser.add_option_group(required) | ||||||
| @ -105,6 +138,10 @@ def main(args): | |||||||
|     if os.path.exists(outputdir): |     if os.path.exists(outputdir): | ||||||
|         parser.error("output directory should not exist.") |         parser.error("output directory should not exist.") | ||||||
| 
 | 
 | ||||||
|  |     opts.logfile = os.path.abspath(opts.logfile) | ||||||
|  | 
 | ||||||
|  |     setup_logging(opts) | ||||||
|  | 
 | ||||||
|     # create the temporary directory for lorax |     # create the temporary directory for lorax | ||||||
|     tempdir = tempfile.mkdtemp(prefix="lorax.", dir=tempfile.gettempdir()) |     tempdir = tempfile.mkdtemp(prefix="lorax.", dir=tempfile.gettempdir()) | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user