def testAppendToLog(self): with open(join(self.tempdir, '2009-11-02-query.log'), 'w') as f: f.write('line0\n') log = DirectoryLog(self.tempdir) log.log(**DEFAULT_KWARGS()) self.assertEquals(['2009-11-02-query.log'], listdir(self.tempdir)) self.assertEquals('line0\n2009-11-02T11:25:36Z 11.22.33.44 1.1K 1.300s 42hits /path query=arguments\n', open(join(self.tempdir, '2009-11-02-query.log')).read())
def testMinimalLog(self): log = DirectoryLog(self.tempdir) log.log(timestamp=1257161136.0) self.assertEquals(['2009-11-02-query.log'], listdir(self.tempdir)) self.assertEquals( '2009-11-02T11:25:36Z - - - - - \n', open(join(self.tempdir, '2009-11-02-query.log')).read())
def testMinimalLog(self): log = DirectoryLog(self.tempdir) log.log( timestamp=1257161136.0 ) self.assertEquals(['2009-11-02-query.log'], listdir(self.tempdir)) self.assertEquals('2009-11-02T11:25:36Z - - - - - \n', open(join(self.tempdir, '2009-11-02-query.log')).read())
def testNewDayNewLogFile(self): kwargs = DEFAULT_KWARGS() kwargs['timestamp'] = 1257161136.0 log = DirectoryLog(self.tempdir) log.log(**kwargs) kwargs['timestamp'] += 24 * 60 * 60 log.log(**kwargs) self.assertEquals(['2009-11-02-query.log', '2009-11-03-query.log'], sorted(listdir(self.tempdir))) self.assertEquals('2009-11-03T11:25:36Z 11.22.33.44 1.1K 1.300s 42hits /path query=arguments\n', open(join(self.tempdir, '2009-11-03-query.log')).read())
def testAppendToLog(self): with open(join(self.tempdir, '2009-11-02-query.log'), 'w') as f: f.write('line0\n') log = DirectoryLog(self.tempdir) log.log(**DEFAULT_KWARGS()) self.assertEquals(['2009-11-02-query.log'], listdir(self.tempdir)) self.assertEquals( 'line0\n2009-11-02T11:25:36Z 11.22.33.44 1.1K 1.300s 42hits /path query=arguments\n', open(join(self.tempdir, '2009-11-02-query.log')).read())
def testRemoveOldLogs(self): nrOfFilesKept = 5 kwargs = DEFAULT_KWARGS() kwargs['timestamp'] = 1257161136.0 for filename in ("%03d-the-end.log" % r for r in range(10)): open(join(self.tempdir, filename), 'w').close() for filename in ("%03d-the-other-end.log" % r for r in range(10)): open(join(self.tempdir, filename), 'w').close() filesBefore = listdir(self.tempdir) log = DirectoryLog(self.tempdir, extension='-the-end.log', nrOfFilesKept=nrOfFilesKept) log.log(**kwargs) filesAfter = listdir(self.tempdir) self.assertFalse('000-the-end.log' in filesAfter) self.assertTrue('000-the-other-end.log' in filesAfter) filesBefore = listdir(self.tempdir) kwargs['timestamp'] += 3600 * 24 log.log(**kwargs) filesAfter = listdir(self.tempdir) self.assertFalse('001' in filesAfter) self.assertEqual(len(filesAfter), len(filesBefore)) with open(join(self.tempdir, '015-the-end.log'), 'w') as fp: pass with open(join(self.tempdir, '016-the-end.log'), 'w') as fp: pass kwargs['timestamp'] += 3600 * 24 log.log(**kwargs) self.assertEqual(5 + 10, len(listdir(self.tempdir)))
def setUp(self): SeecrTestCase.setUp(self) self.logDir = join(self.tempdir, 'log') directoryLog = DirectoryLog(self.logDir) self.qlfs = LogFileServer("Fancy <name>", directoryLog, basepath='/log')
def testRemoveOldLogs(self): nrOfFilesKept = 5 kwargs = DEFAULT_KWARGS() kwargs['timestamp'] = 1257161136.0 for filename in ("%03d-the-end.log" % r for r in range(10)): open(join(self.tempdir, filename), 'w').close() for filename in ("%03d-the-other-end.log" % r for r in range(10)): open(join(self.tempdir, filename), 'w').close() filesBefore = listdir(self.tempdir) log = DirectoryLog(self.tempdir, extension='-the-end.log', nrOfFilesKept=nrOfFilesKept) log.log(**kwargs) filesAfter = listdir(self.tempdir) self.assertFalse('000-the-end.log' in filesAfter) self.assertTrue('000-the-other-end.log' in filesAfter) filesBefore = listdir(self.tempdir) kwargs['timestamp'] += 3600*24 log.log(**kwargs) filesAfter = listdir(self.tempdir) self.assertFalse('001' in filesAfter) self.assertEquals(len(filesAfter), len(filesBefore)) open(join(self.tempdir, '015-the-end.log'), 'w').close() open(join(self.tempdir, '016-the-end.log'), 'w').close() kwargs['timestamp'] += 3600*24 log.log(**kwargs) self.assertEquals(5+10, len(listdir(self.tempdir)))
def setUp(self): SeecrTestCase.setUp(self) self.queryLogWriter = QueryLogWriter(DirectoryLog(self.tempdir)) self.handleRequestLog = HandleRequestLog() self._timeNow = 1257161136.0 # 2009-11-02 11:30:00 def time(): self._timeNow += 1.0 return self._timeNow self.handleRequestLog._time = time
def testAsStream(self): times = [1257161136.0] d = DirectoryLog(self.tempdir) d._now = lambda: times[0] d.write('my line\n') d.flush() self.assertEqual(['2009-11-02-query.log'], listdir(self.tempdir)) with open(join(self.tempdir, '2009-11-02-query.log')) as fp: self.assertEqual('my line\n', fp.read())
def setUp(self): SeecrTestCase.setUp(self) self._timeNow = 1257161136.0 # 2009-11-02 11:30:00 def time(): self._timeNow += 1.0 return self._timeNow directoryLog = DirectoryLog(self.tempdir) self.queryLog = QueryLog(log=directoryLog, loggedPaths=['/path/sru', '/path/srw']) self.queryLog._time = time
def testAsStream(self): times = [1257161136.0] d = DirectoryLog(self.tempdir) d._now = lambda: times[0] d.write('my line\n') d.flush() self.assertEqual(['2009-11-02-query.log'], listdir(self.tempdir)) self.assertEqual('my line\n', open(join(self.tempdir, '2009-11-02-query.log')).read())
def testNewDayNewLogFile(self): kwargs = DEFAULT_KWARGS() kwargs['timestamp'] = 1257161136.0 log = DirectoryLog(self.tempdir) log.log(**kwargs) kwargs['timestamp'] += 24 * 60 * 60 log.log(**kwargs) self.assertEquals(['2009-11-02-query.log', '2009-11-03-query.log'], sorted(listdir(self.tempdir))) self.assertEquals( '2009-11-03T11:25:36Z 11.22.33.44 1.1K 1.300s 42hits /path query=arguments\n', open(join(self.tempdir, '2009-11-03-query.log')).read())
def testSetExtension(self): log = DirectoryLog(self.tempdir, extension='-the-end.log') log.log(**DEFAULT_KWARGS()) self.assertEquals(['2009-11-02-the-end.log'], listdir(self.tempdir))
def testSetExtension(self): log = DirectoryLog(self.tempdir, extension='-the-end.log') log.log(**DEFAULT_KWARGS()) self.assertEqual(['2009-11-02-the-end.log'], listdir(self.tempdir))
def testLogDirCreated(self): logDir = join(self.tempdir, 'amihere') self.assertFalse(isdir(logDir)) DirectoryLog(logDir) self.assertTrue(isdir(logDir))