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);
}
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 + "\"")));
}
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 + "\"")));
}
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 + "\"")));
}
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);
}
}
Aggregations