Search in sources :

Example 41 with Marker

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
    }
}
Also used : Marker(org.apache.logging.log4j.Marker) Test(org.junit.Test)

Example 42 with Marker

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);
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Marker(org.apache.logging.log4j.Marker) Logger(org.apache.logging.log4j.core.Logger)

Example 43 with Marker

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

Example 44 with Marker

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);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) Test(org.junit.Test)

Example 45 with Marker

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);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) Test(org.junit.Test)

Aggregations

Marker (org.apache.logging.log4j.Marker)50 Test (org.junit.Test)30 Message (org.apache.logging.log4j.message.Message)23 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)22 Level (org.apache.logging.log4j.Level)18 LogEvent (org.apache.logging.log4j.core.LogEvent)16 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)14 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)8 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)6 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)6 IOException (java.io.IOException)5 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 MutableThreadContextStack (org.apache.logging.log4j.spi.MutableThreadContextStack)5 StringMap (org.apache.logging.log4j.util.StringMap)5 ThreadContext (org.apache.logging.log4j.ThreadContext)4 Logger (org.apache.logging.log4j.core.Logger)4 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)4 HashMap (java.util.HashMap)3 ThrowableProxy (org.apache.logging.log4j.core.impl.ThrowableProxy)3