Exemple #1
0
    def write(self, record):
        """Note: Assume record begins with a timestamp string."""
        if record is None:
            return

        # First things first: get the date string from the record
        try:
            time_str = record.split(' ')[0]
            ts = timestamp.timestamp(time_str, time_format=self.time_format)
            date_str = timestamp.date_str(ts, date_format=self.date_format)
            logging.debug('LogfileWriter date_str: %s', date_str)
        except ValueError:
            logging.error('LogfileWriter.write() - bad record timestamp: %s',
                          record)
            return

        # Is it time to create a new file to write to?
        if not self.writer or date_str != self.current_date:
            self.current_filename = self.filebase + '-' + date_str
            self.current_date = date_str
            logging.info('LogfileWriter opening new file: %s',
                         self.current_filename)
            self.writer = TextFileWriter(self.current_filename, self.flush)

        logging.debug('LogfileWriter writing record: %s', record)
        self.writer.write(record)
    def test_custom(self):
        transform = TimestampTransform(time_format=timestamp.DATE_FORMAT)

        self.assertIsNone(transform.transform(None))

        result = transform.transform('blah')
        today = timestamp.date_str()
        self.assertEqual(result.split()[0], today)
        self.assertEqual(result.split()[1], 'blah')
    def write(self, record):
        """Note: Assume record begins with a timestamp string."""
        if record is None:
            return

        # If we've got a list, hope it's a list of records. Recurse,
        # calling write() on each of the list elements in order.
        if type(record) is list:
            for single_record in record:
                self.write(single_record)
            return

        if not type(record) is str:
            logging.error(
                'LogfileWriter.write() - record not timestamped: %s ', record)
            return

        # Get the timestamp we'll be using
        try:  # Try to extract timestamp from record
            time_str = record.split(self.split_char)[0]
            ts = timestamp.timestamp(time_str, time_format=self.time_format)
        except ValueError:
            logging.error('LogfileWriter.write() - bad timestamp: %s', record)
            return

        # Now parse ts into hour and date strings
        hr_str = self.rollover_hourly and \
                 timestamp.date_str(ts, date_format='_%H00') or ""
        date_str = timestamp.date_str(ts, date_format=self.date_format)
        logging.debug('LogfileWriter date_str: %s', date_str)

        # Is it time to create a new file to write to?
        if not self.writer or date_str != self.current_date or hr_str != self.current_hour:
            self.current_filename = self.filebase + '-' + date_str + hr_str + self.suffix
            self.current_date = date_str
            self.current_hour = self.rollover_hourly and hr_str or ""
            logging.info('LogfileWriter opening new file: %s',
                         self.current_filename)
            self.writer = FileWriter(filename=self.current_filename,
                                     flush=self.flush)

        logging.debug('LogfileWriter writing record: %s', record)
        self.writer.write(record)
Exemple #4
0
    def write(self, record):
        """Note: Assume record begins with a timestamp string."""
        if record is None:
            return

        # If we've got a list, hope it's a list of records. Recurse,
        # calling write() on each of the list elements in order.
        if type(record) is list:
            for single_record in record:
                self.write(single_record)
            return

        if not type(record) is str:
            logging.error(
                'LogfileWriter.write() - record is not timestamped string: '
                '%s', record)
            return

        # First things first: get the date string from the record
        try:
            time_str = record.split()[0]
            ts = timestamp.timestamp(time_str, time_format=self.time_format)
            hr_str = self.rollover_hourly and timestamp.date_str(
                ts, date_format='_%H00') or ""
            date_str = timestamp.date_str(ts, date_format=self.date_format)
            logging.debug('LogfileWriter date_str: %s', date_str)
        except ValueError:
            logging.error('LogfileWriter.write() - bad record timestamp: %s',
                          record)
            return

        # Is it time to create a new file to write to?
        if not self.writer or date_str != self.current_date or hr_str != self.current_hour:
            self.current_filename = self.filebase + '-' + date_str + hr_str + self.suffix
            self.current_date = date_str
            self.current_hour = self.rollover_hourly and hr_str or ""
            logging.info('LogfileWriter opening new file: %s',
                         self.current_filename)
            self.writer = TextFileWriter(self.current_filename, self.flush)

        logging.debug('LogfileWriter writing record: %s', record)
        self.writer.write(record)
Exemple #5
0
 def test_date_str(self):
     self.assertEqual(timestamp.date_str(1507810403.33), '2017-10-12')
     self.assertEqual(
         timestamp.date_str(1507810403.33, date_format='%Y+%j'), '2017+285')