use of org.apache.logging.log4j.message.StructuredDataMessage 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));
}
use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.
the class StructuredDataLookupTest method testLookup.
@Test
public void testLookup() {
final Message msg = new StructuredDataMessage("Test", "This is a test", "Audit");
final LogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.DEBUG).setMessage(msg).build();
final StrLookup lookup = new StructuredDataLookup();
String value = lookup.lookup(event, TESTKEY);
assertEquals(TESTVAL, value);
value = lookup.lookup("BadKey");
assertNull(value);
}
use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.
the class MarkerPatternConverterTest method testLookup.
@Test
public void testLookup() {
final Message msg = new StructuredDataMessage("Test", "This is a test", "Audit");
final Marker eventMarker = MarkerManager.getMarker("EVENT");
final Marker auditMarker = MarkerManager.getMarker("AUDIT").setParents(eventMarker);
final LogEvent event = Log4jLogEvent.newBuilder().setLoggerName("MyLogger").setMarker(auditMarker).setLevel(Level.DEBUG).setMessage(msg).build();
final StringBuilder sb = new StringBuilder();
final MarkerPatternConverter converter = MarkerPatternConverter.newInstance(null);
converter.format(event, sb);
assertEquals(auditMarker.toString(), sb.toString());
}
use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.
the class MarkerSimpleNamePatternConverterTest method testLookup.
@Test
public void testLookup() {
final Message msg = new StructuredDataMessage("Test", "This is a test", "Audit");
final Marker eventMarker = MarkerManager.getMarker("EVENT");
final Marker auditMarker = MarkerManager.getMarker("AUDIT").setParents(eventMarker);
final LogEvent event = Log4jLogEvent.newBuilder().setLoggerName("MyLogger").setMarker(auditMarker).setLevel(Level.DEBUG).setMessage(msg).build();
final StringBuilder sb = new StringBuilder();
final MarkerSimpleNamePatternConverter converter = MarkerSimpleNamePatternConverter.newInstance(null);
converter.format(event, sb);
assertEquals(auditMarker.getName(), sb.toString());
}
use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.
the class StrictXmlConfigTest 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("Audit@18060", "Transfer Complete", "Transfer");
msg.put("ToAccount", "123456");
msg.put("FromAccount", "123457");
msg.put("Amount", "200.00");
logger.info(MarkerManager.getMarker("EVENT"), msg);
ThreadContext.clearMap();
final List<LogEvent> events = app.getEvents();
assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
Aggregations