use of org.apache.logging.log4j.Marker in project logging-log4j2 by apache.
the class MarkerTest method testAddNullMarker.
@Test
public void testAddNullMarker() {
final String childMarkerName = CHILD_MAKER_NAME + "-ANM";
final String parentMakerName = PARENT_MARKER_NAME + "-ANM";
final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName);
final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName);
slf4jMarker.add(slf4jParent);
final Marker log4jParent = MarkerManager.getMarker(parentMakerName);
final Marker log4jMarker = MarkerManager.getMarker(childMarkerName);
final Log4jMarker log4jSlf4jParent = new Log4jMarker(log4jParent);
final Log4jMarker log4jSlf4jMarker = new Log4jMarker(log4jMarker);
final org.slf4j.Marker nullMarker = null;
try {
log4jSlf4jParent.add(nullMarker);
fail("Expected " + IllegalArgumentException.class.getName());
} catch (final IllegalArgumentException e) {
// expected
}
try {
log4jSlf4jMarker.add(nullMarker);
fail("Expected " + IllegalArgumentException.class.getName());
} catch (final IllegalArgumentException e) {
// expected
}
}
use of org.apache.logging.log4j.Marker in project logging-log4j2 by apache.
the class ScriptAppenderSelectorTest method logAndCheck.
private void logAndCheck() {
final Marker marker = MarkerManager.getMarker("HEXDUMP");
final Logger logger = loggerContextRule.getLogger(ScriptAppenderSelectorTest.class);
logger.error("Hello");
final ListAppender listAppender = getListAppender();
final List<LogEvent> list = listAppender.getEvents();
assertNotNull("No events generated", list);
assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1);
logger.error("World");
assertTrue("Incorrect number of events. Expected 2, got " + list.size(), list.size() == 2);
logger.error(marker, "DEADBEEF");
assertTrue("Incorrect number of events. Expected 3, got " + list.size(), list.size() == 3);
}
use of org.apache.logging.log4j.Marker 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);
}
}
use of org.apache.logging.log4j.Marker in project logging-log4j2 by apache.
the class MarkerLookupTest method testLookupEventNonExistantKey.
@Test
public void testLookupEventNonExistantKey() {
final Marker marker = MarkerManager.getMarker(markerName);
final LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
this.getClass().getName()).setMarker(//
marker).setLoggerFqcn(//
"org.apache.logging.log4j.core.Logger").setLevel(//
Level.INFO).setMessage(new SimpleMessage("Hello, world!")).build();
final String value = strLookup.lookup(event, ABSENT_MARKER_NAME);
assertEquals(markerName, value);
}
use of org.apache.logging.log4j.Marker in project logging-log4j2 by apache.
the class MarkerLookupTest method testLookupEventExistant.
@Test
public void testLookupEventExistant() {
final Marker marker = MarkerManager.getMarker(markerName);
final LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
this.getClass().getName()).setMarker(//
marker).setLoggerFqcn(//
"org.apache.logging.log4j.core.Logger").setLevel(//
Level.INFO).setMessage(new SimpleMessage("Hello, world!")).build();
final String value = strLookup.lookup(event, marker.getName());
assertEquals(markerName, value);
}
Aggregations