Esempio n. 1
0
    def _ensure_file_is_good(self):
        """
        Ensures that the file the reader is tailing is the file it is supposed to be tailing.
        If the target file has been removed, does nothing. If there is a new file in its place, stops tailing the
        current file and tails the new file instead. If the current file position lies past the file's end, resets
        it to the file's beginning.
        """

        try:
            stat_results = os.stat(self.logfile_name)
        except OSError:
            logging.info('The file %s has been removed.', self.logfile_name)
        else:
            expected_logfile_id = self.logfile_id
            actual_logfile_id = get_device_and_inode_string(stat_results)
            current_position = self.logfile.tell()
            file_size = stat_results.st_size

            if expected_logfile_id != actual_logfile_id:
                logging.info('The file %s has been rotated.', self.logfile_name)
                self._close_file()
                self._open_file()
            elif current_position > file_size:
                logging.info('The file %s has been truncated.', self.logfile_name)
                self.logfile.seek(0)
Esempio n. 2
0
 def test_run_seek_first_unprocessed_position(self):
     with prepared_reader(seconds=range(60)) as reader:
         self.write_progress_file('log.log {0} 2250 2250'.format(get_device_and_inode_string(os.stat('log.log'))))
         reader.progress_file_path = 'progressf16c93d1167446f99a26837c0fdeac6fb73869794'
         reader.run()
         self.assertEqual(len(reader.receiver.entries), 31)
         self.assertEqual(reader.receiver.entries[0].timestamp, '2000-01-01 00:00:30,000')
         self.assertEqual(reader.receiver.entries[30], 'EOF 0')
Esempio n. 3
0
 def test_ensure_file_is_good_file_is_good(self):
     with open('log.log', 'wb') as f:
         f.write('Some file contents!')
         f.seek(10)
         reader = LogReader(0, 'log.log', Log4jParser(), FakeReceiver())
         reader.logfile = f
         reader.logfile_id = get_device_and_inode_string(os.fstat(f.fileno()))
         reader._ensure_file_is_good()
         self.assertFalse(f.closed)
         self.assertEqual(reader.logfile, f)
         self.assertEqual(f.tell(), 10)
         self.assertEqual(self.fake_logging.log, [])
Esempio n. 4
0
    def _open_file(self):
        """
        Opens the file the LogReader is responsible for and assigns it to logfile. If that file has the extension ".gz",
        it is opened as a gzip file. Errors are propagated.
        """

        try:
            if self.logfile_name.endswith('.gz'):
                self.logfile = gzip.open(self.logfile_name, 'rb')
            else:
                self.logfile = io.open(self.logfile_name, 'rb')
            logging.info('Opened %s.', self.logfile_name)
        except IOError:
            logging.exception('Failed to open %s.', self.logfile_name)
            raise
        else:
            self.logfile_id = get_device_and_inode_string(os.fstat(self.logfile.fileno()))