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));
}
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);
}
}
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());
}
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());
}
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());
}
Aggregations