コード例 #1
0
    def test_file_attach_handler(self):
        """ Test that a file handler can be attached, and that the logger will produce content to it. """

        # Test that we can attach and write to a local output file.
        logger = Logger()
        logger.attach_file_handler(".", self.LOCAL_OUT_FILE)
        logger.log("Hello")
        logger.log("World")
        self.assertTrue(os.path.exists(self.as_log_file(self.LOCAL_OUT_FILE)))

        # Test that we can attach and write to a global output file.
        Logger.attach_file_handler(".", self.GLOBAL_OUT_FILE)
        Logger.log("How are you?")
        self.assertTrue(os.path.exists(self.as_log_file(self.GLOBAL_OUT_FILE)))

        # Test that these two files have the correct line length.
        with open(self.as_log_file(self.LOCAL_OUT_FILE)) as f:
            self.assertEqual(2, len(f.readlines()))

        with open(self.as_log_file(self.GLOBAL_OUT_FILE)) as f:
            self.assertEqual(1, len(f.readlines()))

        # Test error logging.
        Logger.error("This is an error")
        self.assertTrue(os.path.exists(self.ERROR_FILE))
        with open(self.ERROR_FILE) as f:
            self.assertEqual(1, len(f.readlines()))
コード例 #2
0
    def test_log_file_overwrite(self):
        """ Check that attaching a file handler will clear the previous one. """

        # Create the first file.
        logger = Logger()
        logger.attach_file_handler(".", self.LOCAL_OUT_FILE)
        logger.log("1")
        logger.log("2")
        logger.log("3")
        logger.error("e1")
        logger.error("e2")

        # Now the file should be overwritten.
        logger = Logger()
        logger.attach_file_handler(".", self.LOCAL_OUT_FILE)
        logger.log("1")
        logger.error("e1")

        with open(self.as_log_file(self.LOCAL_OUT_FILE)) as f:
            self.assertEqual(1, len(f.readlines()))

        with open(self.ERROR_FILE) as f:
            self.assertEqual(1, len(f.readlines()))