]> scripts.mit.edu Git - wizard.git/commitdiff
Drastically improve written log output.
authorEdward Z. Yang <ezyang@mit.edu>
Sun, 28 Jun 2009 22:51:45 +0000 (18:51 -0400)
committerEdward Z. Yang <ezyang@mit.edu>
Sun, 28 Jun 2009 22:51:45 +0000 (18:51 -0400)
Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
wizard/command/_base.py
wizard/command/massmigrate.py
wizard/shell.py

index 930326924dc55c545cce5e875bdc29eb936ed239..7d0346be70325f3348e1d119554f1b10031f472a 100644 (file)
@@ -8,14 +8,21 @@ class Error(wizard.Error):
     """Base error class for all command errors"""
     pass
 
-def makeLogger(options):
+def makeLogger(options, numeric_args):
+    context = " ".join(numeric_args)
     logger = logging.getLogger("main")
     logger.setLevel(logging.INFO)
     stdout = logging.StreamHandler(sys.stdout)
-    stdout.setFormatter(logging.Formatter(" " * int(options.indent) + '%(message)s'))
-    logger.addHandler(stdout)
+    stdout.setFormatter(logging.Formatter(" " * int(options.indent) + '%(levelname)s: %(message)s'))
+    dateFormat = "%H:%M:%S"
+    if options.context:
+        logformatter = logging.Formatter("%(asctime)s %(levelname)s -- " + context + ": %(message)s", dateFormat)
+    else:
+        logformatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", dateFormat)
+    if not options.quiet: logger.addHandler(stdout)
     if options.log_file:
         file = logging.FileHandler(options.log_file)
+        file.setFormatter(logformatter)
         logger.addHandler(file)
     if options.debug:
         logger.setLevel(logging.DEBUG)
@@ -31,10 +38,11 @@ def makeBaseArgs(options, **grab):
     """Takes parsed options, and breaks them back into a command
     line string that we can pass into a subcommand"""
     args = []
-    grab["log_file"] = "--log-file"
-    grab["debug"] = "--debug"
+    grab["log_file"]= "--log-file"
+    grab["debug"]   = "--debug"
     grab["verbose"] = "--verbose"
-    grab["indent"] = "--indent"
+    grab["indent"]  = "--indent"
+    grab["quiet"]   = "--quiet"
     #grab["log_db"] = "--log-db"
     for k,flag in grab.items():
         value = getattr(options, k)
@@ -44,6 +52,7 @@ def makeBaseArgs(options, **grab):
             if k == "indent":
                 value += 4
             args.append(str(value))
+    args.append("--context") # always have context for a subcommand
     return args
 
 class NullLogHandler(logging.Handler):
@@ -59,10 +68,14 @@ class WizardOptionParser(optparse.OptionParser):
                 default=False, help="Turns on verbose output")
         self.add_option("--debug", dest="debug", action="store_true",
                 default=False, help="Turns on debugging output")
+        self.add_option("-q", "--quiet", dest="quiet", action="store_true",
+                default=False, help="Turns off output to stdout")
         self.add_option("--log-file", dest="log_file",
                 default=None, help="Logs verbose output to file")
         self.add_option("--indent", dest="indent",
                 default=0, help="Indents stdout, useful for nested calls")
+        self.add_option("--context", dest="context", action="store_true",
+                default=False, help="Adds context to logs, useful for parallel processing")
     def parse_all(self, argv, logger):
         options, numeric_args = self.parse_args(argv)
-        return options, numeric_args, logger and logger or makeLogger(options)
+        return options, numeric_args, logger and logger or makeLogger(options, numeric_args)
index d371ff6d97381d002a91bbbd49eb25fd9d9cfa40..5feaa78172834be36be29b7e09f1f032597ed9c2 100644 (file)
@@ -35,7 +35,7 @@ stat every install to find out if it migrated it yet).
         parser.error("too many arguments")
     elif not args:
         parser.error("must specify application to migrate")
-    if options.verbose or options.dry_run:
+    if options.dry_run:
         options.no_parallelize = True
     app = args[0]
     errors = {}
@@ -74,7 +74,7 @@ stat every install to find out if it migrated it yet).
                     name = e.name
                     if name not in errors: errors[name] = []
                     errors[name].append(d)
-                    logger.error("ERROR [%s] in %s" % (name, d.location))
+                    logger.error("%s in %s" % (name, d.location))
             return (on_success, on_error)
         on_success, on_error = make_on_pair(d)
         sh.wait() # wait for a parallel processing slot to be available
@@ -82,4 +82,4 @@ stat every install to find out if it migrated it yet).
                       user=user, on_success=on_success, on_error=on_error)
     sh.join()
     for name, deploys in errors.items():
-        logger.warning("ERROR [%s] from %d installs" % (name, len(deploys)))
+        logger.warning("%s from %d installs" % (name, len(deploys)))
index 6c08dc35c5082681812a477544256a0f98fdd020..89d01572ea611d1f7f9e1c0f302ec4f3b89ab2fb 100644 (file)
@@ -38,7 +38,7 @@ class Shell(object):
     def call(self, *args, **kwargs):
         kwargs.setdefault("python", None)
         if self.dry or self.logger:
-            self.logger.info("$ " + ' '.join(args))
+            self.logger.info("Running `" + ' '.join(args) + "`")
         if self.dry:
             return
         if kwargs["python"] is None and is_python(args):
@@ -55,8 +55,10 @@ class Shell(object):
             raise eclass(proc.returncode, args, stdout, stderr)
         return (stdout, stderr)
     def log(self, stdout, stderr):
-        if self.logger and stdout: self.logger.info(stdout)
-        if self.logger and stderr: self.logger.info("STDERR: " + stderr)
+        if self.logger and stdout:
+            self.logger.debug("STDOUT: " + stdout)
+        if self.logger and stderr:
+            self.logger.debug("STDERR: " + stderr)
     def callAsUser(self, *args, **kwargs):
         user = kwargs.pop("user", None)
         kwargs.setdefault("python", is_python(args))