def test_file_handler_timestamp(self): self.logging_config['handlers'][0]['timestamp'] = '%F' rlog.configure_logging(self.logging_config) rlog.getlogger().warning('foo') logfile = '%s_%s' % (self.logfile, datetime.now().strftime('%F')) self.assertTrue(os.path.exists(logfile)) os.remove(logfile)
def test_handler_noappend(make_exec_ctx, config_file, logfile): make_exec_ctx( config_file({ 'level': 'info', 'handlers': [{ 'type': 'file', 'name': str(logfile), 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: %(message)s', 'datefmt': '%F', 'append': False, }], 'handlers_perflog': [] })) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile)
def test_global_noconfig(): # This is to test the case when no configuration is set, but since the # order the unit tests are invoked is arbitrary, we emulate the # 'no-config' state by passing `None` to `configure_logging()` rlog.configure_logging(None) assert rlog.getlogger() is rlog.null_logger
def test_handler_level(self): rlog.configure_logging(self.logging_config) rlog.getlogger().info('foo') rlog.getlogger().warning('bar') self.assertFalse(self.found_in_logfile('foo')) self.assertTrue(self.found_in_logfile('bar'))
def test_logging_context(self): rlog.configure_logging(self.logging_config) with rlog.logging_context() as logger: self.assertIs(logger, rlog.getlogger()) self.assertIsNot(logger, rlog.null_logger) rlog.getlogger().error('error from context') self.assertTrue(self.found_in_logfile('reframe')) self.assertTrue(self.found_in_logfile('error from context'))
def test_logging_context(self): rlog.configure_logging(self.logging_config) with rlog.logging_context() as logger: assert logger is rlog.getlogger() assert logger is not rlog.null_logger rlog.getlogger().error('error from context') assert self.found_in_logfile('reframe') assert self.found_in_logfile('error from context')
def test_syslog_handler_no_address(self): self.logging_config = { 'level': 'INFO', 'handlers': [{ 'type': 'syslog' }] } with pytest.raises(ConfigError): rlog.configure_logging(self.logging_config)
def test_file_handler_syntax_no_name(self): self.logging_config = { 'level': 'INFO', 'handlers': [{ 'type': 'file' }], } with pytest.raises(ConfigError): rlog.configure_logging(self.logging_config)
def test_logging_context(default_exec_ctx, logfile): rlog.configure_logging(rt.runtime().site_config) with rlog.logging_context() as logger: assert logger is rlog.getlogger() assert logger is not rlog.null_logger rlog.getlogger().error('error from context') assert _found_in_logfile('reframe', logfile) assert _found_in_logfile('error from context', logfile)
def test_handler_syntax_no_type(self): self.logging_config = { 'level': 'INFO', 'handlers': [{ 'name': 'stderr' }] } with pytest.raises(ConfigError): rlog.configure_logging(self.logging_config)
def test_logging_context_check(self): rlog.configure_logging(self.logging_config) with rlog.logging_context(check=self.check): rlog.getlogger().error('error from context') rlog.getlogger().error('error outside context') assert self.found_in_logfile('_FakeCheck: %s: error from context' % sys.argv[0]) assert self.found_in_logfile('reframe: %s: error outside context' % sys.argv[0])
def test_syslog_handler_unknown_socktype(self): self.logging_config = { 'level': 'INFO', 'handlers': [{ 'type': 'syslog', 'socktype': 'foo' }] } with pytest.raises(ConfigError): rlog.configure_logging(self.logging_config)
def test_multiple_handlers(self): self.logging_config = { 'level': 'INFO', 'handlers': { '&1': {}, self.logfile: {}, } } rlog.configure_logging(self.logging_config) self.assertEqual(len(rlog.getlogger().logger.handlers), 2)
def test_warn_once(default_exec_ctx, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo', cache=True) rlog.getlogger().warning('foo', cache=True) rlog.getlogger().warning('foo', cache=True) _flush_handlers() _close_handlers() with open(logfile, 'rt') as fp: assert len(re.findall('foo', fp.read())) == 1
def test_logging_context_check(default_exec_ctx, logfile, fake_check): rlog.configure_logging(rt.runtime().site_config) with rlog.logging_context(check=fake_check): rlog.getlogger().error('error from context') rlog.getlogger().error('error outside context') assert _found_in_logfile(f'_FakeCheck: {sys.argv[0]}: error from context', logfile) assert _found_in_logfile(f'reframe: {sys.argv[0]}: error outside context', logfile)
def test_logging_context_check(self): rlog.configure_logging(self.logging_config) with rlog.logging_context(check=self.check): rlog.getlogger().error('error from context') rlog.getlogger().error('error outside context') self.assertTrue( self.found_in_logfile('random_check: error from context')) self.assertTrue( self.found_in_logfile('reframe: error outside context'))
def test_handler_append(default_exec_ctx, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile)
def test_handler_append(self): rlog.configure_logging(self.logging_config) rlog.getlogger().warning('foo') self.close_handlers() # Reload logger rlog.configure_logging(self.logging_config) rlog.getlogger().warning('bar') self.assertTrue(self.found_in_logfile('foo')) self.assertTrue(self.found_in_logfile('bar'))
def test_logging_context_error(self): rlog.configure_logging(self.logging_config) try: with rlog.logging_context(level=rlog.ERROR): raise ReframeError('error from context') self.fail('logging_context did not propagate the exception') except ReframeError: pass self.assertTrue(self.found_in_logfile('reframe')) self.assertTrue(self.found_in_logfile('error from context'))
def test_logging_context_error(default_exec_ctx, logfile): rlog.configure_logging(rt.runtime().site_config) try: with rlog.logging_context(level=rlog.ERROR): raise ReframeError('error from context') pytest.fail('logging_context did not propagate the exception') except ReframeError: pass assert _found_in_logfile('reframe', logfile) assert _found_in_logfile('error from context', logfile)
def test_syslog_handler_tcp_port_noint(temp_runtime): runtime = temp_runtime({ 'level': 'info', 'handlers': [{ 'type': 'syslog', 'address': 'foo.server.org:bar', }], 'handlers_perflog': [] }) next(runtime) with pytest.raises(ConfigError, match="not an integer: 'bar'"): rlog.configure_logging(rt.runtime().site_config)
def test_stream_handler_unknown_stream(self): self.logging_config = { 'level': 'INFO', 'handlers': [ { 'type': 'stream', 'name': 'foo' }, ], } with pytest.raises(ConfigError): rlog.configure_logging(self.logging_config)
def test_multiple_handlers(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ {'type': 'stream', 'name': 'stderr'}, {'type': 'file', 'name': str(logfile)}, {'type': 'syslog', 'address': '/dev/log'} ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) assert len(rlog.getlogger().logger.handlers) == 3
def test_multiple_handlers(self): self.logging_config = { 'level': 'INFO', 'handlers': [{ 'type': 'stream', 'name': 'stderr' }, { 'type': 'file', 'name': self.logfile }], } rlog.configure_logging(self.logging_config) self.assertEqual(len(rlog.getlogger().logger.handlers), 2)
def test_stream_handler_stdout(self): self.logging_config = { 'level': 'INFO', 'handlers': { '&1': {}, } } rlog.configure_logging(self.logging_config) raw_logger = rlog.getlogger().logger self.assertEqual(len(raw_logger.handlers), 1) handler = raw_logger.handlers[0] self.assertIsInstance(handler, logging.StreamHandler) self.assertEqual(handler.stream, sys.stdout)
def test_stream_handler_stdout(self): self.logging_config = { 'level': 'INFO', 'handlers': [{ 'type': 'stream', 'name': 'stdout' }], } rlog.configure_logging(self.logging_config) raw_logger = rlog.getlogger().logger assert len(raw_logger.handlers) == 1 handler = raw_logger.handlers[0] assert isinstance(handler, logging.StreamHandler) assert handler.stream == sys.stdout
def test_stream_handler(temp_runtime, logfile, stream): runtime = temp_runtime({ 'level': 'info', 'handlers': [{'type': 'stream', 'name': stream}], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) raw_logger = rlog.getlogger().logger assert len(raw_logger.handlers) == 1 handler = raw_logger.handlers[0] assert isinstance(handler, logging.StreamHandler) stream = sys.stdout if stream == 'stdout' else sys.stderr assert handler.stream == stream
def test_stream_handler_stderr(self): self.logging_config = { 'level': 'INFO', 'handlers': [{ 'type': 'stream', 'name': 'stderr' }], } rlog.configure_logging(self.logging_config) raw_logger = rlog.getlogger().logger self.assertEqual(len(raw_logger.handlers), 1) handler = raw_logger.handlers[0] self.assertIsInstance(handler, logging.StreamHandler) self.assertEqual(handler.stream, sys.stderr)
def test_multiple_handlers(self): self.logging_config = { 'level': 'INFO', 'handlers': [{ 'type': 'stream', 'name': 'stderr' }, { 'type': 'file', 'name': self.logfile }, { 'type': 'syslog', 'address': '/dev/log' }], } rlog.configure_logging(self.logging_config) assert len(rlog.getlogger().logger.handlers) == 3
def test_syslog_handler(temp_runtime): import platform if platform.system() == 'Linux': addr = '/dev/log' elif platform.system() == 'Darwin': addr = '/var/run/syslog' else: pytest.skip('unknown system platform') runtime = temp_runtime({ 'level': 'info', 'handlers': [{'type': 'syslog', 'address': addr}], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().info('foo')