Java Code Examples for org.apache.logging.log4j.core.Logger#removeAppender()

The following examples show how to use org.apache.logging.log4j.core.Logger#removeAppender() . 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: 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 2
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);
    }
}
 
Example 3
Source File: FlumeAppenderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testStructured() throws IOException {
    final Agent[] agents = new Agent[] { Agent.createAgent("localhost",
            testPort) };
    final FlumeAppender avroAppender = FlumeAppender.createAppender(agents,
            null, null, "false", "Avro", null, "1000", "1000", "1", "1000",
            "avro", "false", null, null, null, "ReqCtx_", null, "true",
            "1", null, null, null, null);
    avroAppender.start();
    final Logger eventLogger = (Logger) LogManager.getLogger("EventLogger");
    Assert.assertNotNull(eventLogger);
    eventLogger.addAppender(avroAppender);
    eventLogger.setLevel(Level.ALL);

    final StructuredDataMessage msg = new StructuredDataMessage("Transfer",
            "Success", "Audit");
    msg.put("memo", "This is a memo");
    msg.put("acct", "12345");
    msg.put("amount", "100.00");
    ThreadContext.put("id", UUID.randomUUID().toString());
    ThreadContext.put("memo", null);
    ThreadContext.put("test", "123");

    EventLogger.logEvent(msg);

    final Transaction transaction = channel.getTransaction();
    transaction.begin();

    final Event event = channel.take();
    Assert.assertNotNull(event);
    Assert.assertTrue("Channel contained event, but not expected message", getBody(event).endsWith("Success"));
    transaction.commit();
    transaction.close();

    eventSource.stop();
    eventLogger.removeAppender(avroAppender);
    avroAppender.stop();
}
 
Example 4
Source File: ReconfigureAppenderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void removeAppender()
{
	Logger logger = (Logger)LogManager.getLogger(this.getClass());

	// This call attempts to remove the file manager, but uses the name of the appender
	// (NULL in this case) instead of PATTERN.
	// see AbstractManager.stop(...).
	appender.stop();
	logger.removeAppender(appender);
}
 
Example 5
Source File: ConsoleAppender.java    From DiscordSRV with GNU General Public License v3.0 4 votes vote down vote up
public void shutdown() {
    Logger rootLogger = (Logger) LogManager.getRootLogger();
    rootLogger.removeAppender(this);
}
 
Example 6
Source File: TestAppender.java    From iaf with Apache License 2.0 4 votes vote down vote up
public static void removeAppender(TestAppender appender) {
	Logger logger = getRootLogger();
	logger.removeAppender(appender);
}
 
Example 7
Source File: CsvParameterLayoutTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private static void removeAppenders(final Logger root) {
    final Map<String, Appender> appenders = root.getAppenders();
    for (final Appender appender : appenders.values()) {
        root.removeAppender(appender);
    }
}