Java Code Examples for java.util.logging.StreamHandler#flush()

The following examples show how to use java.util.logging.StreamHandler#flush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReportingTest.java    From vertx-unit with Apache License 2.0 5 votes vote down vote up
private String testLog(String name, Runnable runnable) {
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  StreamHandler handler = new StreamHandler(buffer, new java.util.logging.SimpleFormatter());
  Logger logger = Logger.getLogger(name);
  logger.addHandler(handler);
  try {
    runnable.run();
    handler.flush();
    return buffer.toString();
  } finally {
    logger.removeHandler(handler);
  }
}
 
Example 2
Source File: EngintTaskLoggerTest.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
public void testLogger( ) throws Exception
{
	Logger engineLogger = Logger.getAnonymousLogger( );
	ByteArrayOutputStream engineOut = new ByteArrayOutputStream( );
	StreamHandler engineHandler = new StreamHandler( engineOut,
			new SimpleFormatter( ) );
	engineHandler.setLevel( Level.ALL );
	engineLogger.addHandler( engineHandler );
	engineLogger.setLevel( Level.ALL );

	Logger taskLogger = Logger.getAnonymousLogger();
	ByteArrayOutputStream taskOut = new ByteArrayOutputStream( );
	StreamHandler taskHandler = new StreamHandler( taskOut,
			new SimpleFormatter( ) );
	taskHandler.setLevel( Level.ALL );
	taskLogger.addHandler( taskHandler );
	taskLogger.setLevel( Level.ALL );

	engine.setLogger( engineLogger );
	engine.changeLogLevel( Level.ALL );

	engineHandler.flush( );
	taskHandler.flush( );

	new File( "./utest/" ).mkdirs( );
	copyResource(
			"org/eclipse/birt/report/engine/api/engine-task-logger-test.rptdesign",
			"./utest/reportdesign.rptdesign" );
	IReportRunnable runnable = engine
			.openReportDesign( "./utest/reportdesign.rptdesign" );

	IRunTask task = engine.createRunTask( runnable );
	task.setParameter( "sample", "==golden values==", "displayText" );
	task.run( "./utest/report.rptdocument" );
	task.close( );

	// the logger is output to engine out.
	engineHandler.flush( );
	assertTrue( engineOut.toString( ).indexOf( "==golden values==" ) != -1 );
	engineOut.reset( );

	task = engine.createRunTask( runnable );
	task.setLogger( taskLogger );
	task.setParameter( "sample", "==golden values==", "displayText" );
	task.run( "./utest/report.rptdocument" );
	task.close( );

	engineHandler.flush( );
	taskHandler.flush( );
	assertTrue( engineOut.toString( ).indexOf( "==golden values==" ) == -1 );
	assertTrue( taskOut.toString( ).indexOf( "==golden values==" ) != -1 );
}