def test_stale_stream(tmpdir): # make mock filestream with write/flush that goes stale after 100 writes # create logger with stream emitter to mocked file stream class StalewardFile(file): def __init__(self, *a, **kw): file.__init__(self, *a, **kw) self._write_count = 0 def write(self, *a, **kw): self._write_count += 1 if self._write_count > 100: exc = IOError('stale file handle') exc.errno = errno.ESTALE self.close() raise exc return file.write(self, *a, **kw) file_path = '%s/not_always_fresh.log' % (tmpdir,) stale_file_obj = StalewardFile(file_path, 'wb') emitter = StreamEmitter(stale_file_obj) sink = SensibleSink(SF('{status_char} - {iso_end}'), emitter, filters=[SensibleFilter(success=True)]) logger = Logger('excelsilog', [sink]) assert emitter.stream is stale_file_obj for i in range(200): logger.info('yay').success() lines = open(file_path).read().splitlines() assert len(lines) == 200 assert len(lines[0]) == len(lines[-1]) assert stale_file_obj.closed assert emitter.stream is not stale_file_obj
def _test_exception(): _tmpl = ('{iso_end} - {exc_type}: {exc_message}' ' - {func_name}:{line_number} - {exc_tb_list}') sink = SensibleSink(SF(_tmpl), StreamEmitter('stderr'), filters=[SensibleFilter(exception=0)]) logger = Logger('excelsilog', [sink]) with logger.info('A for Effort', reraise=False) as tr: print tr raise ValueError('E for Exception') return
def _test_exception(): _tmpl = ('{iso_end} - {exc_type}: {exc_message}' ' - {func_name}:{line_number} - {exc_tb_list}') sink = SensibleSink(SF(_tmpl), StreamEmitter('stderr'), filters=[SensibleFilter(exception=False)]) logger = Logger('excelsilog', [sink]) with logger.info('A for Effort', reraise=False) as tr: print tr raise ValueError('E for Exception') return
def test_stale_stream(tmpdir): # make mock filestream with write/flush that goes stale after 100 writes # create logger with stream emitter to mocked file stream class StalewardFile(file): def __init__(self, *a, **kw): file.__init__(self, *a, **kw) self._write_count = 0 def write(self, *a, **kw): self._write_count += 1 if self._write_count > 100: exc = IOError('stale file handle') exc.errno = errno.ESTALE self.close() raise exc return file.write(self, *a, **kw) file_path = '%s/not_always_fresh.log' % (tmpdir, ) stale_file_obj = StalewardFile(file_path, 'wb') emitter = StreamEmitter(stale_file_obj) sink = SensibleSink(SF('{status_char} - {iso_end}'), emitter, filters=[SensibleFilter(success=True)]) logger = Logger('excelsilog', [sink]) assert emitter.stream is stale_file_obj for i in range(200): logger.info('yay').success() lines = open(file_path).read().splitlines() assert len(lines) == 200 assert len(lines[0]) == len(lines[-1]) assert stale_file_obj.closed assert emitter.stream is not stale_file_obj
def test_rate_sink(): sink = RateSink() logger = Logger('testlog', sinks=[sink]) for i in range(10): with logger.info('sleeping', reraise=False): time.sleep(0.02) if i % 2: raise ValueError() test_rates = sink.get_rates()['testlog']['sleeping'] assert 48 <= round(test_rates['__all__']) <= 51 assert 24 <= round(test_rates['exception']) <= 26 counts = sink.get_total_counts() assert counts['__all__'] == 10 assert repr(sink)
def test_rate_sink(): sink = RateSink() logger = Logger('testlog', sinks=[sink]) for i in range(10): with logger.info('sleeping', reraise=False): time.sleep(0.02) if i % 2: raise ValueError() test_rates = sink.get_rates()['testlog']['sleeping'] # get_rates() returns per-second rates for logger invocations. These # results are hardware dependent for the lower bound, but the upper bound # is well defined because of the hard coded sleep() call above. assert 40 <= round(test_rates['__all__']) <= 51 assert 20 <= round(test_rates['exception']) <= 26 counts = sink.get_total_counts() assert counts['__all__'] == 10 assert repr(sink)
def test_rate_sink(): sink = RateSink() logger = Logger('testlog', sinks=[sink]) for i in range(10): with logger.info('sleeping', reraise=False): time.sleep(0.02) if i % 2: raise ValueError() test_rates = sink.get_rates()['testlog']['sleeping'] # TODO: these are a little flaky, esp when moving between # environments, runtimes, and with/without coverage, hence the # range all_lower_limit = 40 if IS_PYPY else 48 assert all_lower_limit <= round(test_rates['__all__']) <= 51 assert 22 <= round(test_rates['exception']) <= 26 counts = sink.get_total_counts() assert counts['__all__'] == 10 assert repr(sink)