Example usage for java.util.logging StreamHandler close

List of usage examples for java.util.logging StreamHandler close

Introduction

In this page you can find the example usage for java.util.logging StreamHandler close.

Prototype

@Override
public synchronized void close() throws SecurityException 

Source Link

Document

Close the current output stream.

Usage

From source file:android.net.http.CookiesTest.java

/**
 * Test that we don't log potentially sensitive cookie values.
 * http://b/3095990/*from   w  w w.j av a 2s.c  om*/
 */
public void testCookiesAreNotLogged() throws IOException, URISyntaxException {
    // enqueue an HTTP response with a cookie that will be rejected
    server.enqueue(new MockResponse().addHeader("Set-Cookie: password=secret; Domain=fake.domain"));
    server.play();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Logger logger = Logger.getLogger("org.apache.http");
    StreamHandler handler = new StreamHandler(out, new SimpleFormatter());
    logger.addHandler(handler);
    try {
        HttpClient client = new DefaultHttpClient();
        client.execute(new HttpGet(server.getUrl("/").toURI()));
        handler.close();

        String log = out.toString("UTF-8");
        assertTrue(log, log.contains("password"));
        assertTrue(log, log.contains("fake.domain"));
        assertFalse(log, log.contains("secret"));

    } finally {
        logger.removeHandler(handler);
    }
}

From source file:com.bc.fiduceo.post.PostProcessingToolTest.java

@Test
public void testThatPostProcessingToolInCaseOfExceptionsPrintsTheErrorMessageAndContinueWithTheNextFile()
        throws Exception {
    final ArrayList<Path> mmdFiles = new ArrayList<>();
    mmdFiles.add(Paths.get("nonExistingFileOne"));
    mmdFiles.add(Paths.get("nonExistingFileTwo"));
    mmdFiles.add(Paths.get("nonExistingFileThree"));

    final Formatter formatter = new SimpleFormatter();
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    final StreamHandler handler = new StreamHandler(stream, formatter);
    final Logger logger = FiduceoLogger.getLogger();
    FiduceoLogger.setLevelSilent();/*www  .  j a v a2  s  . co  m*/

    try {
        logger.addHandler(handler);

        final PostProcessingContext context = new PostProcessingContext();
        final PostProcessingConfig processingConfig = getConfig();
        context.setProcessingConfig(processingConfig);

        final PostProcessingTool postProcessingTool = new PostProcessingTool(context);

        postProcessingTool.computeFiles(mmdFiles);

        handler.close();
        final String string = stream.toString();
        assertThat(string, CoreMatchers.containsString("nonExistingFileOne"));
        assertThat(string, CoreMatchers.containsString("nonExistingFileTwo"));
        assertThat(string, CoreMatchers.containsString("nonExistingFileThree"));
    } finally {
        logger.removeHandler(handler);
    }
}

From source file:org.apache.zeppelin.lens.LensInterpreter.java

private InterpreterResult HandleHelp(JLineShell shell, String st) {
    java.util.logging.StreamHandler sh = null;
    java.util.logging.Logger springLogger = null;
    java.util.logging.Formatter formatter = new java.util.logging.Formatter() {
        public String format(java.util.logging.LogRecord record) {
            return record.getMessage();
        }/*w w w .j a v  a 2  s  . c  o  m*/
    };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        sh = new java.util.logging.StreamHandler(baos, formatter);
        springLogger = HandlerUtils.getLogger(org.springframework.shell.core.SimpleParser.class);
        springLogger.addHandler(sh);
        shell.executeCommand(st);
    } catch (Exception e) {
        s_logger.error(e.getMessage(), e);
        return new InterpreterResult(Code.ERROR, e.getMessage());
    } finally {
        sh.flush();
        springLogger.removeHandler(sh);
        sh.close();
    }
    return new InterpreterResult(Code.SUCCESS, baos.toString());
}