Search in sources :

Example 11 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class EventLoggerTest method structuredData.

@Test
public void structuredData() {
    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");
    EventLogger.logEvent(msg);
    ThreadContext.clearMap();
    assertEquals(1, results.size());
    final String expected = "EVENT OFF Audit [Transfer@18060 Amount=\"200.00\" FromAccount=\"123457\" ToAccount=\"123456\"] Transfer Complete";
    assertThat("Incorrect structured data", results.get(0), startsWith(expected));
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) Test(org.junit.Test)

Example 12 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class Rfc5424Layout method appendMessage.

private void appendMessage(final StringBuilder buffer, final LogEvent event) {
    final Message message = event.getMessage();
    // This layout formats StructuredDataMessages instead of delegating to the Message itself.
    final String text = (message instanceof StructuredDataMessage) ? message.getFormat() : message.getFormattedMessage();
    if (text != null && text.length() > 0) {
        buffer.append(' ').append(escapeNewlines(text, escapeNewLine));
    }
    if (exceptionFormatters != null && event.getThrown() != null) {
        final StringBuilder exception = new StringBuilder(LF);
        for (final PatternFormatter formatter : exceptionFormatters) {
            formatter.format(event, exception);
        }
        buffer.append(escapeNewlines(exception.toString(), escapeNewLine));
    }
    if (includeNewLine) {
        buffer.append(LF);
    }
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) Message(org.apache.logging.log4j.message.Message) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) PatternFormatter(org.apache.logging.log4j.core.pattern.PatternFormatter)

Example 13 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class RoutingAppenderWithJndiTest method routingTest.

@Test
public void routingTest() throws NamingException {
    // default route when there's no jndi resource
    StructuredDataMessage msg = new StructuredDataMessage("Test", "This is a message from unknown context", "Context");
    EventLogger.logEvent(msg);
    final File defaultLogFile = new File("target/routingbyjndi/routingbyjnditest-unknown.log");
    assertTrue("The default log file was not created", defaultLogFile.exists());
    // now set jndi resource to Application1
    final Context context = new InitialContext();
    context.bind(JNDI_CONTEXT_NAME, "Application1");
    msg = new StructuredDataMessage("Test", "This is a message from Application1", "Context");
    EventLogger.logEvent(msg);
    assertNotNull("No events generated", listAppender1.getEvents());
    assertTrue("Incorrect number of events. Expected 1, got " + listAppender1.getEvents().size(), listAppender1.getEvents().size() == 1);
    // now set jndi resource to Application2
    context.rebind(JNDI_CONTEXT_NAME, "Application2");
    msg = new StructuredDataMessage("Test", "This is a message from Application2", "Context");
    EventLogger.logEvent(msg);
    assertNotNull("No events generated", listAppender2.getEvents());
    assertTrue("Incorrect number of events. Expected 1, got " + listAppender2.getEvents().size(), listAppender2.getEvents().size() == 1);
    assertTrue("Incorrect number of events. Expected 1, got " + listAppender1.getEvents().size(), listAppender1.getEvents().size() == 1);
    msg = new StructuredDataMessage("Test", "This is another message from Application2", "Context");
    EventLogger.logEvent(msg);
    assertNotNull("No events generated", listAppender2.getEvents());
    assertTrue("Incorrect number of events. Expected 2, got " + listAppender2.getEvents().size(), listAppender2.getEvents().size() == 2);
    assertTrue("Incorrect number of events. Expected 1, got " + listAppender1.getEvents().size(), listAppender1.getEvents().size() == 1);
    // now set jndi resource to Application3.
    // The context name, 'Application3', will be used as log file name by the default route.
    context.rebind("java:comp/env/logging/context-name", "Application3");
    msg = new StructuredDataMessage("Test", "This is a message from Application3", "Context");
    EventLogger.logEvent(msg);
    final File application3LogFile = new File("target/routingbyjndi/routingbyjnditest-Application3.log");
    assertTrue("The Application3 log file was not created", application3LogFile.exists());
    // now set jndi resource to Application4
    // The context name, 'Application4', will be used as log file name by the default route.
    context.rebind("java:comp/env/logging/context-name", "Application4");
    msg = new StructuredDataMessage("Test", "This is a message from Application4", "Context");
    EventLogger.logEvent(msg);
    final File application4LogFile = new File("target/routingbyjndi/routingbyjnditest-Application4.log");
    assertTrue("The Application3 log file was not created", application4LogFile.exists());
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) File(java.io.File) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 14 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class RoutingAppenderWithPurgingTest method routingTest.

@Test(timeout = 5000)
public void routingTest() throws InterruptedException {
    StructuredDataMessage msg = new StructuredDataMessage("1", "This is a test 1", "Service");
    EventLogger.logEvent(msg);
    final List<LogEvent> list = app.getEvents();
    assertNotNull("No events generated", list);
    assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1);
    msg = new StructuredDataMessage("2", "This is a test 2", "Service");
    EventLogger.logEvent(msg);
    msg = new StructuredDataMessage("3", "This is a test 3", "Service");
    EventLogger.logEvent(msg);
    final String[] files = { IDLE_LOG_FILE1, IDLE_LOG_FILE2, IDLE_LOG_FILE3, MANUAL_LOG_FILE1, MANUAL_LOG_FILE2, MANUAL_LOG_FILE3 };
    assertFileExistance(files);
    assertEquals("Incorrect number of appenders with IdlePurgePolicy.", 3, routingAppenderIdle.getAppenders().size());
    assertEquals("Incorrect number of appenders with IdlePurgePolicy with HangingAppender.", 3, routingAppenderIdleWithHangingAppender.getAppenders().size());
    assertEquals("Incorrect number of appenders manual purge.", 3, routingAppenderManual.getAppenders().size());
    Thread.sleep(3000);
    EventLogger.logEvent(msg);
    assertEquals("Incorrect number of appenders with IdlePurgePolicy.", 1, routingAppenderIdle.getAppenders().size());
    assertEquals("Incorrect number of appenders with manual purge.", 3, routingAppenderManual.getAppenders().size());
    routingAppenderManual.deleteAppender("1");
    routingAppenderManual.deleteAppender("2");
    routingAppenderManual.deleteAppender("3");
    assertEquals("Incorrect number of appenders with IdlePurgePolicy.", 1, routingAppenderIdle.getAppenders().size());
    assertEquals("Incorrect number of appenders with manual purge.", 0, routingAppenderManual.getAppenders().size());
    msg = new StructuredDataMessage("5", "This is a test 5", "Service");
    EventLogger.logEvent(msg);
    assertEquals("Incorrect number of appenders with manual purge.", 1, routingAppenderManual.getAppenders().size());
    routingAppenderManual.deleteAppender("5");
    routingAppenderManual.deleteAppender("5");
    assertEquals("Incorrect number of appenders with manual purge.", 0, routingAppenderManual.getAppenders().size());
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) Test(org.junit.Test)

Example 15 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class RoutingDefaultAppenderTest method routingTest.

@Test
public void routingTest() {
    StructuredDataMessage msg = new StructuredDataMessage("Test", "This is a test", "Service");
    EventLogger.logEvent(msg);
    final List<LogEvent> list = loggerContextRule.getListAppender("List").getEvents();
    assertNotNull("No events generated", list);
    assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1);
    msg = new StructuredDataMessage("Test", "This is a test", "Alert");
    EventLogger.logEvent(msg);
    final File file = new File(LOG_FILE);
    assertTrue("Alert file was not created", file.exists());
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) File(java.io.File) Test(org.junit.Test)

Aggregations

StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)40 Test (org.junit.Test)32 Event (org.apache.flume.Event)12 AvroFlumeEvent (org.apache.flume.source.avro.AvroFlumeEvent)11 LogEvent (org.apache.logging.log4j.core.LogEvent)11 File (java.io.File)6 Message (org.apache.logging.log4j.message.Message)6 EventLogger (org.apache.logging.log4j.EventLogger)5 Logger (org.apache.logging.log4j.Logger)4 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)4 StatusLogger (org.apache.logging.log4j.status.StatusLogger)4 Appender (org.apache.logging.log4j.core.Appender)3 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)3 Map (java.util.Map)2 Marker (org.apache.logging.log4j.Marker)2 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)2 MapMessage (org.apache.logging.log4j.message.MapMessage)2 HashMap (java.util.HashMap)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1