def setUp(self): import sys import StringIO self.stderr_str = StringIO.StringIO() sys.stderr = self.stderr_str self.logger = Logger()
class TestLogger(unittest.TestCase): def setUp(self): import sys import StringIO self.stderr_str = StringIO.StringIO() sys.stderr = self.stderr_str self.logger = Logger() def test_log_format(self): self.assertEqual(self.logger.format_log("sessionid", "logclass", 3, "message"), "None[0]: logclass(3): (sessionid): message") self.assertEqual(self.logger.format_log(None, "logclass", 3, "message"), "None[0]: logclass(3): (nosession): message") self.assertEqual(self.logger.format_log(None, "logclass", 3, "%s %s", ("printf-style", "message")), "None[0]: logclass(3): (nosession): printf-style message") def test_log(self): self.logger.init("test", use_syslog=False) self.logger.log("sessionid", "log.class", 3, "message") self.assertEqual(self.stderr_str.getvalue(), "test[%d]: log.class(3): (sessionid): message\n" % (os.getpid())) def test_log_format_type_error(self): self.logger.init("test", use_syslog=False) self.logger.log(None, "log.class", 3, "%s", None) self.assertEqual(self.stderr_str.getvalue(), "test[%d]: log.class(3): (nosession): %s\n" % (os.getpid(), '%s')) def test_log_format_value_error(self): self.logger.init("test", use_syslog=False) self.logger.log(None, "log.class", 3, "%s", ("format", "error")) self.assertEqual(self.stderr_str.getvalue(), ("test[%d]: core.error(3): (nosession): Unable to format log message; error='not all arguments converted during string formatting'\n" + "test[%d]: log.class(3): (nosession): %s\n") % (os.getpid(), os.getpid(), '%s') ) def test_uninitialized_state(self): with self.assertRaises(ValueError): self.logger.log("sessionid", "log.class", 3, "message") with self.assertRaises(ValueError): self.logger.use_syslog = False