Search in sources :

Example 76 with Appender

use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.

the class OutputStreamAppenderTest method addAppender.

/**
     * Tests that you can add an output stream appender dynamically.
     */
private void addAppender(final OutputStream outputStream, final String outputStreamName) {
    final LoggerContext context = LoggerContext.getContext(false);
    final Configuration config = context.getConfiguration();
    final PatternLayout layout = PatternLayout.createDefaultLayout(config);
    final Appender appender = OutputStreamAppender.createAppender(layout, null, outputStream, outputStreamName, false, true);
    appender.start();
    config.addAppender(appender);
    ConfigurationTestUtils.updateLoggers(appender, config);
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Configuration(org.apache.logging.log4j.core.config.Configuration) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 77 with Appender

use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.

the class HttpAppenderTest method testAppendHttps.

@Test
public void testAppendHttps() throws Exception {
    wireMockRule.stubFor(post(urlEqualTo("/test/log4j/")).willReturn(SUCCESS_RESPONSE));
    final Appender appender = HttpAppender.newBuilder().withName("Http").withLayout(JsonLayout.createDefaultLayout()).setConfiguration(ctx.getConfiguration()).setUrl(new URL("https://localhost:" + wireMockRule.httpsPort() + "/test/log4j/")).setSslConfiguration(SslConfiguration.createSSLConfiguration(null, KeyStoreConfiguration.createKeyStoreConfiguration(TestConstants.KEYSTORE_FILE, TestConstants.KEYSTORE_PWD, TestConstants.KEYSTORE_TYPE, null), TrustStoreConfiguration.createKeyStoreConfiguration(TestConstants.TRUSTSTORE_FILE, TestConstants.TRUSTSTORE_PWD, TestConstants.TRUSTSTORE_TYPE, null))).setVerifyHostname(false).build();
    appender.append(createLogEvent());
    wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/")).withHeader("Host", containing("localhost")).withHeader("Content-Type", containing("application/json")).withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\"")));
}
Also used : Appender(org.apache.logging.log4j.core.Appender) URL(java.net.URL) Test(org.junit.Test)

Example 78 with Appender

use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.

the class HttpAppenderTest method testAppend.

@Test
public void testAppend() throws Exception {
    wireMockRule.stubFor(post(urlEqualTo("/test/log4j/")).willReturn(SUCCESS_RESPONSE));
    final Appender appender = HttpAppender.newBuilder().withName("Http").withLayout(JsonLayout.createDefaultLayout()).setConfiguration(ctx.getConfiguration()).setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/")).build();
    appender.append(createLogEvent());
    wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/")).withHeader("Host", containing("localhost")).withHeader("Content-Type", containing("application/json")).withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\"")));
}
Also used : Appender(org.apache.logging.log4j.core.Appender) URL(java.net.URL) Test(org.junit.Test)

Example 79 with Appender

use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.

the class HttpAppenderTest method testAppendMethodPut.

@Test
public void testAppendMethodPut() throws Exception {
    wireMockRule.stubFor(put(urlEqualTo("/test/log4j/1234")).willReturn(SUCCESS_RESPONSE));
    final Appender appender = HttpAppender.newBuilder().withName("Http").withLayout(JsonLayout.createDefaultLayout()).setConfiguration(ctx.getConfiguration()).setMethod("PUT").setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/1234")).build();
    appender.append(createLogEvent());
    wireMockRule.verify(putRequestedFor(urlEqualTo("/test/log4j/1234")).withHeader("Host", containing("localhost")).withHeader("Content-Type", containing("application/json")).withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\"")));
}
Also used : Appender(org.apache.logging.log4j.core.Appender) URL(java.net.URL) Test(org.junit.Test)

Example 80 with Appender

use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.

the class HtmlLayoutTest method testLayout.

private void testLayout(final boolean includeLocation) throws Exception {
    final Map<String, Appender> appenders = root.getAppenders();
    for (final Appender appender : appenders.values()) {
        root.removeAppender(appender);
    }
    // set up appender
    final HtmlLayout layout = HtmlLayout.newBuilder().withLocationInfo(includeLocation).build();
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    // set appender on root and set level to debug
    root.addAppender(appender);
    root.setLevel(Level.DEBUG);
    // output starting message
    root.debug("starting mdc pattern test");
    root.debug("empty mdc");
    root.debug("First line\nSecond line");
    ThreadContext.put("key1", "value1");
    ThreadContext.put("key2", "value2");
    root.debug("filled mdc");
    ThreadContext.remove("key1");
    ThreadContext.remove("key2");
    root.error("finished mdc pattern test", new NullPointerException("test"));
    appender.stop();
    final List<String> list = appender.getMessages();
    final StringBuilder sb = new StringBuilder();
    for (final String string : list) {
        sb.append(string);
    }
    final String html = sb.toString();
    assertTrue("Incorrect number of lines. Require at least 85 " + list.size(), list.size() > 85);
    final String string = list.get(3);
    assertTrue("Incorrect header: " + string, string.equals("<meta charset=\"UTF-8\"/>"));
    assertTrue("Incorrect title", list.get(4).equals("<title>Log4j Log Messages</title>"));
    assertTrue("Incorrect footer", list.get(list.size() - 1).equals("</body></html>"));
    if (includeLocation) {
        assertTrue("Incorrect multiline", list.get(50).equals(multiLine));
        assertTrue("Missing location", html.contains("HtmlLayoutTest.java:"));
        assertTrue("Incorrect body", list.get(71).equals(body));
    } else {
        assertFalse("Location should not be in the output table", html.contains("<td>HtmlLayoutTest.java:"));
    }
    for (final Appender app : appenders.values()) {
        root.addAppender(app);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender)

Aggregations

Appender (org.apache.logging.log4j.core.Appender)98 Test (org.junit.Test)50 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)25 Configuration (org.apache.logging.log4j.core.config.Configuration)21 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)18 LoggerContext (org.apache.logging.log4j.core.LoggerContext)15 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)13 Logger (org.apache.logging.log4j.Logger)12 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)12 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)11 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)9 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)9 Map (java.util.Map)8 Filter (org.apache.logging.log4j.core.Filter)8 AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)8 URL (java.net.URL)7 IOException (java.io.IOException)5 LogEvent (org.apache.logging.log4j.core.LogEvent)5 Logger (org.apache.logging.log4j.core.Logger)5 AsyncAppender (org.apache.logging.log4j.core.appender.AsyncAppender)5