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

The following examples show how to use java.util.logging.Handler#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: ProcessScopeTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldLogExceptionStacktrace() throws IOException {
  Logger logger = Logger.getLogger(ProcessScope.class.getName());
  try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
    Handler handler = new StreamHandler(out, new SimpleFormatter());
    logger.addHandler(handler);
    try {
      ProcessScope scope = new ProcessScope();
      Object variable = scope.get("testObject", null);
      assertNull(variable);
    } finally {
      handler.flush();
      handler.close();
      logger.removeHandler(handler);
    }
    // test for logged exception
    String message = new String(out.toByteArray(), StandardCharsets.UTF_8);
    assertTrue(!message.isEmpty());
    assertTrue(message.contains("org.camunda.bpm.engine.spring.components.scope.ProcessScope get"));
    assertTrue(message.contains("couldn't return value from process scope! java.lang.NullPointerException"));
    assertTrue(message.contains("at org.camunda.bpm.engine.spring.components.scope.ProcessScope.getExecutionId(ProcessScope.java:")); 
    assertTrue(message.contains("at org.camunda.bpm.engine.spring.components.scope.ProcessScope.getConversationId(ProcessScope.java:")); 
    assertTrue(message.contains("at org.camunda.bpm.engine.spring.components.scope.ProcessScope.get(ProcessScope.java:")); 
    assertTrue(message.contains("at org.camunda.bpm.engine.spring.test.components.scope.ProcessScopeTest.shouldLogExceptionStacktrace(ProcessScopeTest.java:")); 
  }
}
 
Example 2
Source File: TopLogging.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Allows tests to flush all standard handlers */
static void flush(boolean clear) {
    System.err.flush();


    Handler s = streamHandler;
    if (s != null) {
        s.flush();
    }

    Handler d = defaultHandler;
    if (d != null) {
        d.flush();
    }

    if (clear) {
        streamHandler = null;
        defaultHandler = null;
    }
}
 
Example 3
Source File: ExceptionTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Test PayaraException with no parameters specified throwing
 * and logging.
 */
@Test
public void testPayaraExceptionWithNothing() {
    // this message must match PayaraIdeException() constructor
    // log message.
    String gfieMsg = "Caught PayaraIdeException.";
    java.util.logging.Logger logger = Logger.getLogger();
    Level logLevel = logger.getLevel();
    OutputStream logOut = new ByteArrayOutputStream(256);
    Handler handler = new StreamHandler(logOut, new SimpleFormatter());
    handler.setLevel(Level.WARNING);
    logger.addHandler(handler);       
    logger.setLevel(Level.WARNING);
    try {
        throw new PayaraIdeException();
    } catch (PayaraIdeException gfie) {
        handler.flush();
    } finally {
        logger.removeHandler(handler);
        handler.close();
        logger.setLevel(logLevel);
    }
    String logMsg = logOut.toString();
    int contains = logMsg.indexOf(gfieMsg);
    assertTrue(contains > -1);
}
 
Example 4
Source File: ExceptionTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Test PayaraException with message throwing and logging.
 */
@Test
public void testPayaraExceptionWithMsg() {
    String gfieMsg = "Test exception";
    java.util.logging.Logger logger = Logger.getLogger();
    Level logLevel = logger.getLevel();
    OutputStream logOut = new ByteArrayOutputStream(256);
    Handler handler = new StreamHandler(logOut, new SimpleFormatter());
    handler.setLevel(Level.WARNING);
    logger.addHandler(handler);       
    logger.setLevel(Level.WARNING);
    try {
        throw new PayaraIdeException(gfieMsg);
    } catch (PayaraIdeException gfie) {
        handler.flush();
    } finally {
        logger.removeHandler(handler);
        handler.close();
        logger.setLevel(logLevel);
    }
    String logMsg = logOut.toString();
    int contains = logMsg.indexOf(gfieMsg);
    assertTrue(contains > -1);
}
 
Example 5
Source File: ExceptionTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Test GlassFishException with no parameters specified throwing
 * and logging.
 */
@Test
public void testGlassFishExceptionWithNothing() {
    // this message must match GlassFishIdeException() constructor
    // log message.
    String gfieMsg = "Caught GlassFishIdeException.";
    java.util.logging.Logger logger = Logger.getLogger();
    Level logLevel = logger.getLevel();
    OutputStream logOut = new ByteArrayOutputStream(256);
    Handler handler = new StreamHandler(logOut, new SimpleFormatter());
    handler.setLevel(Level.WARNING);
    logger.addHandler(handler);       
    logger.setLevel(Level.WARNING);
    try {
        throw new GlassFishIdeException();
    } catch (GlassFishIdeException gfie) {
        handler.flush();
    } finally {
        logger.removeHandler(handler);
        handler.close();
        logger.setLevel(logLevel);
    }
    String logMsg = logOut.toString();
    int contains = logMsg.indexOf(gfieMsg);
    assertTrue(contains > -1);
}
 
Example 6
Source File: ExceptionTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Test GlassFishException with message throwing and logging.
 */
@Test
public void testGlassFishExceptionWithMsg() {
    String gfieMsg = "Test exception";
    java.util.logging.Logger logger = Logger.getLogger();
    Level logLevel = logger.getLevel();
    OutputStream logOut = new ByteArrayOutputStream(256);
    Handler handler = new StreamHandler(logOut, new SimpleFormatter());
    handler.setLevel(Level.WARNING);
    logger.addHandler(handler);       
    logger.setLevel(Level.WARNING);
    try {
        throw new GlassFishIdeException(gfieMsg);
    } catch (GlassFishIdeException gfie) {
        handler.flush();
    } finally {
        logger.removeHandler(handler);
        handler.close();
        logger.setLevel(logLevel);
    }
    String logMsg = logOut.toString();
    int contains = logMsg.indexOf(gfieMsg);
    assertTrue(contains > -1);
}
 
Example 7
Source File: ExceptionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Test GlassFishException with message and cause <code>Throwable</code>
 * throwing and logging.
 */
@Test
public void testGlassFishExceptionWithMsgAndCause() {
    String gfieMsg = "Test exception";
    String causeMsg = "Cause exception";
    java.util.logging.Logger logger = Logger.getLogger();
    Level logLevel = logger.getLevel();
    OutputStream logOut = new ByteArrayOutputStream(256);
    Handler handler = new StreamHandler(logOut, new SimpleFormatter());
    handler.setLevel(Level.WARNING);
    logger.addHandler(handler);       
    logger.setLevel(Level.WARNING);
    try {
        try {
            throw new Exception(causeMsg);
        } catch (Exception e) {
            throw new GlassFishIdeException(gfieMsg, e);
        }
    } catch (GlassFishIdeException gfie) {
        handler.flush();
    } finally {
        logger.removeHandler(handler);
        handler.close();
        logger.setLevel(logLevel);
    }
    String logMsg = logOut.toString();
    int containsGfieMsg = logMsg.indexOf(gfieMsg);
    int containsCauseMsg = logMsg.indexOf(causeMsg);
    assertTrue(containsGfieMsg > -1 && containsCauseMsg > -1);
}
 
Example 8
Source File: JUnitRunner.java    From buck with Apache License 2.0 5 votes vote down vote up
private void flushAndRemoveLogHandler(Logger rootLogger, Handler handler) {
  if (handler != null) {
    handler.flush();
  }
  if (rootLogger != null && handler != null) {
    rootLogger.removeHandler(handler);
  }
}
 
Example 9
Source File: LogConfig.java    From buck with Apache License 2.0 5 votes vote down vote up
public static void flushLogs() {
  Logger rootLogger = LogManager.getLogManager().getLogger("");
  if (rootLogger == null) {
    return;
  }
  Handler[] handlers = rootLogger.getHandlers();
  if (handlers == null) {
    return;
  }
  for (Handler h : handlers) {
    h.flush();
  }
}
 
Example 10
Source File: EngineLogger.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public void flush( )
{
	if ( fileHandlers != null )
	{
		for ( Handler handler : fileHandlers )
		{
			handler.flush();
		}
	}
}
 
Example 11
Source File: DebugLoggerConfigurator.java    From bazel with Apache License 2.0 5 votes vote down vote up
/** Flushes all loggers at com.google.devtools.build.* or higher. */
public static void flushServerLog() {
  for (Logger logger = templateLogger; logger != null; logger = logger.getParent()) {
    for (Handler handler : logger.getHandlers()) {
      if (handler != null) {
        handler.flush();
      }
    }
  }
}
 
Example 12
Source File: Tools.java    From Zettelkasten with GNU General Public License v3.0 5 votes vote down vote up
/**
 *
 */
public static void flushSessionLog() {
    Handler[] handlers = Constants.zknlogger.getHandlers();
    for (Handler h : handlers) {
        if (h.toString().contains("StreamHandler")) {
            h.flush();
        }
    }
}
 
Example 13
Source File: LevelHandler.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Flush the handler.
 */
public void flush()
{
  int level = getLevel().intValue();
  
  for (int i = 0; i < _handlers.length; i++) {
    Handler handler = _handlers[i];

    if (level <= handler.getLevel().intValue())
      handler.flush();
  }
}
 
Example 14
Source File: LoggingMetricExporter.java    From opentelemetry-java with Apache License 2.0 5 votes vote down vote up
/**
 * Flushes the data.
 *
 * @return the result of the operation
 */
@Override
public ResultCode flush() {
  ResultCode resultCode = ResultCode.SUCCESS;
  for (Handler handler : logger.getHandlers()) {
    try {
      handler.flush();
    } catch (Throwable t) {
      resultCode = ResultCode.FAILURE;
    }
  }
  return resultCode;
}
 
Example 15
Source File: ExceptionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Test PayaraException with message and cause <code>Throwable</code>
 * throwing and logging.
 */
@Test
public void testPayaraExceptionWithMsgAndCause() {
    String gfieMsg = "Test exception";
    String causeMsg = "Cause exception";
    java.util.logging.Logger logger = Logger.getLogger();
    Level logLevel = logger.getLevel();
    OutputStream logOut = new ByteArrayOutputStream(256);
    Handler handler = new StreamHandler(logOut, new SimpleFormatter());
    handler.setLevel(Level.WARNING);
    logger.addHandler(handler);       
    logger.setLevel(Level.WARNING);
    try {
        try {
            throw new Exception(causeMsg);
        } catch (Exception e) {
            throw new PayaraIdeException(gfieMsg, e);
        }
    } catch (PayaraIdeException gfie) {
        handler.flush();
    } finally {
        logger.removeHandler(handler);
        handler.close();
        logger.setLevel(logLevel);
    }
    String logMsg = logOut.toString();
    int containsGfieMsg = logMsg.indexOf(gfieMsg);
    int containsCauseMsg = logMsg.indexOf(causeMsg);
    assertTrue(containsGfieMsg > -1 && containsCauseMsg > -1);
}
 
Example 16
Source File: LoggingTestBase.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
protected void flush(Logger log) {
    ApiProxy.flushLogs();
    for (Handler handler : log.getHandlers()) {
        handler.flush();
    }
}
 
Example 17
Source File: LogsTooEarlyTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void flush() {
    for (Handler h : Lookup.getDefault().lookupAll(Handler.class)) {
        h.flush();
    }
}
 
Example 18
Source File: TopLogging.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void flush() {
    for (Handler h : instances) {
        h.flush();
    }
}
 
Example 19
Source File: LoggingBuildListener.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
  for (Handler h : LOG.getHandlers()) {
    h.flush();
  }
}
 
Example 20
Source File: ConsoleHandlerTest.java    From buck with Apache License 2.0 4 votes vote down vote up
private static void publishAndFlush(Handler handler, LogRecord logRecord) {
  handler.publish(logRecord);
  handler.flush();
}