Search in sources :

Example 81 with Appender

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

the class JsonLayoutTest method testEscapeLayout.

@Test
public void testEscapeLayout() throws Exception {
    final Map<String, Appender> appenders = this.rootLogger.getAppenders();
    for (final Appender appender : appenders.values()) {
        this.rootLogger.removeAppender(appender);
    }
    final Configuration configuration = rootLogger.getContext().getConfiguration();
    // set up appender
    final boolean propertiesAsList = false;
    // @formatter:off
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setConfiguration(configuration).setLocationInfo(true).setProperties(true).setPropertiesAsList(propertiesAsList).setComplete(true).setCompact(false).setEventEol(false).setIncludeStacktrace(true).build();
    // @formatter:on
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    // set appender on root and set level to debug
    this.rootLogger.addAppender(appender);
    this.rootLogger.setLevel(Level.DEBUG);
    // output starting message
    this.rootLogger.debug("Here is a quote ' and then a double quote \"");
    appender.stop();
    final List<String> list = appender.getMessages();
    this.checkAt("[", 0, list);
    this.checkAt("{", 1, list);
    this.checkContains("\"level\" : \"DEBUG\",", list);
    this.checkContains("\"message\" : \"Here is a quote ' and then a double quote \\\"\",", list);
    this.checkContains("\"loggerFqcn\" : \"" + AbstractLogger.class.getName() + "\",", list);
    for (final Appender app : appenders.values()) {
        this.rootLogger.addAppender(app);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Example 82 with Appender

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

the class Rfc5424LayoutTest method testParameterizedMessage.

@Test
public void testParameterizedMessage() {
    for (final Appender appender : root.getAppenders().values()) {
        root.removeAppender(appender);
    }
    // set up appender
    final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, "RequestContext", null, null, true, null, "ATM", null, "key1, key2, locale", null, null, null, true, null, null);
    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);
    root.info("Hello {}", "World");
    try {
        final List<String> list = appender.getMessages();
        assertTrue("Not enough list entries", list.size() > 0);
        final String message = list.get(0);
        assertTrue("Incorrect message. Expected - Hello World, Actual - " + message, message.contains("Hello World"));
    } finally {
        root.removeAppender(appender);
        appender.stop();
    }
}
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) Test(org.junit.Test)

Example 83 with Appender

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

the class SerializedLayoutTest method testLayout.

/**
     * Test case for MDC conversion pattern.
     */
@Test
public void testLayout() throws Exception {
    final Map<String, Appender> appenders = root.getAppenders();
    for (final Appender appender : appenders.values()) {
        root.removeAppender(appender);
    }
    // set up appender
    final SerializedLayout layout = SerializedLayout.createLayout();
    final ListAppender appender = new ListAppender("List", null, layout, false, true);
    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");
    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"));
    final Exception parent = new IllegalStateException("Test");
    final Throwable child = new LoggingException("This is a test", parent);
    root.error("Throwing an exception", child);
    appender.stop();
    final List<byte[]> data = appender.getData();
    assertTrue(data.size() > 0);
    int i = 0;
    for (final byte[] item : data) {
        final ByteArrayInputStream bais = new ByteArrayInputStream(item);
        final ObjectInputStream ois = new ObjectInputStream(bais);
        LogEvent event;
        try {
            event = (LogEvent) ois.readObject();
        } catch (final IOException ioe) {
            System.err.println("Exception processing item " + i);
            throw ioe;
        }
        assertTrue("Incorrect event", event.toString().equals(expected[i]));
        ++i;
    }
    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) LoggingException(org.apache.logging.log4j.LoggingException) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) IOException(java.io.IOException) LoggingException(org.apache.logging.log4j.LoggingException) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 84 with Appender

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

the class SyslogLayoutTest method testLayout.

/**
     * Test case for MDC conversion pattern.
     */
@Test
public void testLayout() throws Exception {
    for (final Appender appender : root.getAppenders().values()) {
        root.removeAppender(appender);
    }
    // set up appender
    // @formatter:off
    final SyslogLayout layout = SyslogLayout.newBuilder().setFacility(Facility.LOCAL0).setIncludeNewLine(true).build();
    // @formatter:on
    //ConsoleAppender appender = new ConsoleAppender("Console", layout);
    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");
    ThreadContext.put("key1", "value1");
    ThreadContext.put("key2", "value2");
    root.debug("filled mdc");
    ThreadContext.put("loginId", "JohnDoe");
    ThreadContext.put("ipAddress", "192.168.0.120");
    ThreadContext.put("locale", Locale.US.getDisplayName());
    final StructuredDataMessage msg = new StructuredDataMessage("Transfer@18060", "Transfer Complete", "Audit");
    msg.put("ToAccount", "123456");
    msg.put("FromAccount", "123457");
    msg.put("Amount", "200.00");
    root.info(MarkerManager.getMarker("EVENT"), msg);
    appender.stop();
    final List<String> list = appender.getMessages();
    assertTrue("Expected line 1 to end with: " + line1 + " Actual " + list.get(0), list.get(0).endsWith(line1));
    assertTrue("Expected line 2 to end with: " + line2 + " Actual " + list.get(1), list.get(1).endsWith(line2));
    assertTrue("Expected line 3 to end with: " + line3 + " Actual " + list.get(2), list.get(2).endsWith(line3));
    assertTrue("Expected line 4 to end with: " + line4 + " Actual " + list.get(3), list.get(3).endsWith(line4));
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 85 with Appender

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

the class XmlLayoutTest method testLayout.

/**
     * Test case for MDC conversion pattern.
     */
@Test
public void testLayout() throws Exception {
    final Map<String, Appender> appenders = this.rootLogger.getAppenders();
    for (final Appender appender : appenders.values()) {
        this.rootLogger.removeAppender(appender);
    }
    // set up appender
    final XmlLayout layout = XmlLayout.createLayout(true, true, true, false, null, true);
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    // set appender on root and set level to debug
    this.rootLogger.addAppender(appender);
    this.rootLogger.setLevel(Level.DEBUG);
    // output starting message
    this.rootLogger.debug("starting mdc pattern test");
    this.rootLogger.debug("empty mdc");
    ThreadContext.put("key1", "value1");
    ThreadContext.put("key2", "value2");
    this.rootLogger.debug("filled mdc");
    ThreadContext.remove("key1");
    ThreadContext.remove("key2");
    this.rootLogger.error("finished mdc pattern test", new NullPointerException("test"));
    final Marker marker = MarkerManager.getMarker("EVENT");
    this.rootLogger.error(marker, "marker test");
    appender.stop();
    final List<String> list = appender.getMessages();
    final String string = list.get(0);
    assertTrue("Incorrect header: " + string, string.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    assertTrue("Incorrect footer", list.get(list.size() - 1).equals("</Events>"));
    this.checkContains("loggerFqcn=\"" + AbstractLogger.class.getName() + "\"", list);
    this.checkContains("level=\"DEBUG\"", list);
    this.checkContains(">starting mdc pattern test</Message>", list);
    // this.checkContains("<Message>starting mdc pattern test</Message>", list);
    // <Marker xmlns="" _class="org.apache.logging.log4j.MarkerManager..Log4jMarker" name="EVENT"/>
    this.checkContains("<Marker", list);
    this.checkContains("name=\"EVENT\"/>", list);
    for (final Appender app : appenders.values()) {
        this.rootLogger.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) Marker(org.apache.logging.log4j.Marker) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

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