Ejemplo n.º 1
0
    def test_file_logger(self):
        tmp_dir = tempfile.mkdtemp()
        try:
            log_name = "test"
            log_file_name = os.path.join(tmp_dir,
                                         "log-{}.txt".format(log_name))

            self.assertTrue(os.path.isdir(tmp_dir))
            self.assertFalse(os.path.exists(log_file_name))

            with file_logger.FileLogger(tmp_dir, log_name) as logger:
                logger.print("line 1")
                logger.print("line", 2)
                logger.print("line", 3, "asdf")

            with open(log_file_name, "r") as f:
                lines = f.readlines()

                self.assertLen(lines, 3)
                self.assertIn("line 1", lines[0])
                self.assertIn("line 2", lines[1])
                self.assertIn("line 3 asdf", lines[2])
        finally:
            if os.path.exists(log_file_name):
                os.remove(log_file_name)
            os.rmdir(tmp_dir)
Ejemplo n.º 2
0
 def _watcher(*, config, num=None, **kwargs):
   """Wrap the decorated function."""
   name = fn.__name__
   if num is not None:
     name += "-" + str(num)
   with file_logger.FileLogger(config.path, name, config.quiet) as logger:
     print("{} started".format(name))
     logger.print("{} started".format(name))
     try:
       return fn(config=config, logger=logger, **kwargs)
     except Exception as e:
       logger.print("\n".join([
           "",
           " Exception caught ".center(60, "="),
           traceback.format_exc(),
           "=" * 60,
       ]))
       print("Exception caught in {}: {}".format(name, e))
       raise
     finally:
       logger.print("{} exiting".format(name))
       print("{} exiting".format(name))