Commits

Ryan Dale  committed 28b413a

add optional timestamp to logging output

  • Participants
  • Parent commits 082ad27

Comments (0)

Files changed (2)

File snakemake/__init__.py

     debug=False,
     notemp=False,
     nodeps=False,
-    jobscript=None):
+    jobscript=None,
+    timestamp=False):
     """
     Run snakemake on a given snakefile.
     Note: at the moment, this function is not thread-safe!
     lock              -- lock the working directory
     """
 
-    init_logger(nocolor=nocolor, stdout=dryrun, debug=debug)
+    init_logger(nocolor=nocolor, stdout=dryrun, debug=debug, timestamp=timestamp)
 
     if not os.path.exists(snakefile):
         logger.error("Error: Snakefile \"{}\" not present.".format(snakefile))
                         debug=debug,
                         notemp=notemp,
                         nodeps=nodeps,
-                        jobscript=jobscript)
+                        jobscript=jobscript,
+                        timestamp=timestamp)
                     for subworkflow in workflow.subworkflows:
                         logger.warning("Executing subworkflow {}.".format(subworkflow.name))
                         if not subsnakemake(subworkflow.snakefile, workdir=subworkflow.workdir, targets=subworkflow.targets):
         "--debug", action="store_true", help="Print debugging output.")
     parser.add_argument(
         "--version", "-v", action="version", version=__version__)
+    parser.add_argument(
+        '--timestamp', '-T', action='store_true',
+        help='Add a timestamp to all logging output')
 
     args = parser.parse_args()
 
             print_compilation=args.print_compilation,
             debug=args.debug,
             jobscript=args.jobscript,
-            notemp=args.notemp)
+            notemp=args.notemp,
+            timestamp=args.timestamp)
     sys.exit(0 if success else 1)

File snakemake/logging.py

 
 import logging
 import platform
+import time
 import sys
 from multiprocessing import Lock
 
 __author__ = "Johannes Köster"
 
 
+
 class ColorizingStreamHandler(logging.StreamHandler):
     _output_lock = Lock()
 
         'ERROR': RED
     }
 
-    def __init__(self, nocolor=False, stream=sys.stderr):
+    def __init__(self, nocolor=False, stream=sys.stderr, timestamp=False):
         super().__init__(stream=stream)
         self.nocolor = nocolor
+        self.timestamp = timestamp
 
     @property
     def is_tty(self):
         if (not self.nocolor
             and record.levelname in self.colors
             and platform.system() != 'Windows'):
-            return "{color}{message}{reset}".format(
+            fmt_dict = dict(
                 color=self.COLOR_SEQ % (30 + self.colors[record.levelname]),
                 message=record.message,
                 reset=self.RESET_SEQ
             )
-        return record.message
+            if self.timestamp:
+                return "{color}[{time}] {message}{reset}".format(
+                    time=time.asctime(),
+                    **fmt_dict)
+            else:
+                    return "{color}{message}{reset}".format(
+                        color=self.COLOR_SEQ % (30 + self.colors[record.levelname]),
+                        message=record.message,
+                        reset=self.RESET_SEQ
+                    )
+        if not self.timestamp:
+            return record.message
+        return "[{time}] {message}".format(
+            time=time.asctime(),
+            message=record.message)
 
 logger = logging.getLogger(__name__)
 handler = None
 
 
-def init_logger(nocolor=False, stdout=False, debug=False):
+def init_logger(nocolor=False, stdout=False, debug=False, timestamp=False):
     global logger
     global handler
     if handler:
         logger.removeHandler(handler)
     handler = ColorizingStreamHandler(
-        nocolor=nocolor, stream=sys.stdout if stdout else sys.stderr)
+        nocolor=nocolor, stream=sys.stdout if stdout else sys.stderr,
+        timestamp=timestamp
+    )
     logger.addHandler(handler)
     logger.setLevel(logging.DEBUG if debug else logging.INFO)