Java Code Examples for org.apache.logging.log4j.core.appender.WriterAppender#start()

The following examples show how to use org.apache.logging.log4j.core.appender.WriterAppender#start() . 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: LoggerStringWriter.java    From servicetalk with Apache License 2.0 7 votes vote down vote up
private static StringWriter addWriterAppender(final LoggerContext context, Level level) {
    final Configuration config = context.getConfiguration();
    final StringWriter writer = new StringWriter();

    final Map.Entry<String, Appender> existing = config.getAppenders().entrySet().iterator().next();
    final WriterAppender writerAppender = WriterAppender.newBuilder()
            .setName(APPENDER_NAME)
            .setLayout(existing.getValue().getLayout())
            .setTarget(writer)
            .build();

    writerAppender.start();
    config.getRootLogger().addAppender(writerAppender, level, null);

    return writer;
}
 
Example 2
Source File: DebugFrameImplTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
void loggedCalls() throws Exception {
    final URL url = getClass().getResource("debugFrameImplTest.html");
    final String expectedLog = IOUtils.toString(getClass().getResourceAsStream("debugFrameImplTest.txt"),
            ISO_8859_1);

    final StringWriter stringWriter = new StringWriter();
    final PatternLayout layout = PatternLayout.newBuilder().withPattern("%msg%n").build();

    final WriterAppender writerAppender = WriterAppender.newBuilder().setName("writeLogger").setTarget(stringWriter)
            .setLayout(layout).build();
    writerAppender.start();

    loggerDebugFrameImpl_.addAppender(writerAppender);
    try {
        client_.getPage(url);
    }
    finally {
        loggerDebugFrameImpl_.removeAppender(writerAppender);
    }

    assertEquals(expectedLog, stringWriter.toString());
}
 
Example 3
Source File: DefaultCredentialsProvider2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that on calling the website twice, only the first time unauthorized response is returned.
 *
 * @throws Exception if an error occurs
 */
@Test
public void basicAuthentication_singleAuthenticaiton() throws Exception {
    final Logger logger = (Logger) LogManager.getLogger("org.apache.http.headers");
    final Level oldLevel = logger.getLevel();
    Configurator.setLevel(logger.getName(), Level.DEBUG);

    final StringWriter stringWriter = new StringWriter();
    final PatternLayout layout = PatternLayout.newBuilder().withPattern("%msg%n").build();

    final WriterAppender writerAppender = WriterAppender.newBuilder().setName("writeLogger").setTarget(stringWriter)
            .setLayout(layout).build();
    writerAppender.start();

    logger.addAppender(writerAppender);
    try {
        ((DefaultCredentialsProvider) getWebClient().getCredentialsProvider()).addCredentials("jetty", "jetty");

        loadPage("Hi There");
        int unauthorizedCount = StringUtils.countMatches(stringWriter.toString(), "HTTP/1.1 401");
        assertEquals(1, unauthorizedCount);

        // and again
        loadPage("Hi There");
        unauthorizedCount = StringUtils.countMatches(stringWriter.toString(), "HTTP/1.1 401");
        assertEquals(1, unauthorizedCount);
    }
    finally {
        logger.removeAppender(writerAppender);
        Configurator.setLevel(logger.getName(), oldLevel);
    }
}
 
Example 4
Source File: HttpWebConnectionTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if an error occurs
 */
@Test
@Alerts(DEFAULT = "Host",
        IE = {})
public void hostHeaderFirst() throws Exception {
    final Logger logger = (Logger) LogManager.getLogger("org.apache.http.headers");
    final Level oldLevel = logger.getLevel();
    Configurator.setLevel(logger.getName(), Level.DEBUG);

    final StringWriter stringWriter = new StringWriter();
    final PatternLayout layout = PatternLayout.newBuilder().withPattern("%msg%n").build();

    final WriterAppender writerAppender = WriterAppender.newBuilder().setName("writeLogger").setTarget(stringWriter)
            .setLayout(layout).build();
    writerAppender.start();

    logger.addAppender(writerAppender);
    try {
        startWebServer("./");

        final WebClient webClient = getWebClient();
        webClient.getPage(URL_FIRST + "LICENSE.txt");
        final String[] messages = StringUtils.split(stringWriter.toString(), "\n");
        for (int i = 0; i < getExpectedAlerts().length; i++) {
            assertTrue(messages[i + 1].contains(getExpectedAlerts()[i]));
        }
    }
    finally {
        logger.removeAppender(writerAppender);
        Configurator.setLevel(logger.getName(), oldLevel);
    }
}