Java Code Examples for org.apache.logging.log4j.MarkerManager#getMarker()

The following examples show how to use org.apache.logging.log4j.MarkerManager#getMarker() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Log4jMarkerFactory.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private static org.apache.logging.log4j.Marker convertMarker(final Marker original,
                                                             final Collection<Marker> visited) {
    final org.apache.logging.log4j.Marker marker = MarkerManager.getMarker(original.getName());
    if (original.hasReferences()) {
        final Iterator<Marker> it = original.iterator();
        while (it.hasNext()) {
            final Marker next = it.next();
            if (visited.contains(next)) {
                LOGGER.warn("Found a cycle in Marker [{}]. Cycle will be broken.", next.getName());
            } else {
                visited.add(next);
                marker.addParents(convertMarker(next, visited));
            }
        }
    }
    return marker;
}
 
Example 2
Source File: Log4jMarkerFactory.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private static org.apache.logging.log4j.Marker convertMarker(final Marker original,
                                                             final Collection<Marker> visited) {
    final org.apache.logging.log4j.Marker marker = MarkerManager.getMarker(original.getName());
    if (original.hasReferences()) {
        final Iterator<Marker> it = original.iterator();
        while (it.hasNext()) {
            final Marker next = it.next();
            if (visited.contains(next)) {
                LOGGER.warn("Found a cycle in Marker [{}]. Cycle will be broken.", next.getName());
            } else {
                visited.add(next);
                marker.addParents(convertMarker(next, visited));
            }
        }
    }
    return marker;
}
 
Example 3
Source File: NoMarkerFilterTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMarkers() {
    final Marker sampleMarker = MarkerManager.getMarker("SampleMarker");
    NoMarkerFilter filter = NoMarkerFilter.newBuilder().build();
    filter.start();
    assertTrue(filter.isStarted());
    assertSame(Filter.Result.DENY, filter.filter(null, null, sampleMarker, (Object) null, (Throwable) null));
    assertSame(Filter.Result.NEUTRAL, filter.filter(null, null, null, (Object) null, (Throwable) null));
    filter.stop();
    LogEvent event = Log4jLogEvent.newBuilder() //
            .setLevel(Level.DEBUG) //
            .setMessage(new SimpleMessage("Hello, world!")).build();
    assertSame(Filter.Result.NEUTRAL, filter.filter(event));

    filter.start();
    assertSame(Filter.Result.NEUTRAL, filter.filter(event));
    event = Log4jLogEvent.newBuilder() //
            .setMarker(sampleMarker) //
            .setLevel(Level.DEBUG) //
            .setMessage(new SimpleMessage("Hello, world!")).build();
    assertSame(Filter.Result.DENY, filter.filter(event));
    filter.stop();
}
 
Example 4
Source File: MarkerAttributeConverterTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testConvert01() {
    final Marker marker = MarkerManager.getMarker("testConvert01");

    final String converted = this.converter.convertToDatabaseColumn(marker);

    assertNotNull("The converted value should not be null.", converted);
    assertEquals("The converted value is not correct.", "testConvert01", converted);

    final Marker reversed = this.converter.convertToEntityAttribute(converted);

    assertNotNull("The reversed value should not be null.", reversed);
    assertEquals("The reversed value is not correct.", "testConvert01", marker.getName());
}
 
Example 5
Source File: XMLConfigLogIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenXMLConfigurationPlugin_whenUsingFlowMarkers_ThenLogsCorrectly() throws Exception {
    Logger logger = LogManager.getLogger(this.getClass());
    Marker markerContent = MarkerManager.getMarker("FLOW");
    logger.debug(markerContent, "Debug log message");
    logger.info(markerContent, "Info log message");
    logger.error(markerContent, "Error log message");
}
 
Example 6
Source File: AbstractMarkerMixInTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testOneParent() throws IOException {
    final Marker expected = MarkerManager.getMarker("A");
    final Marker parent = MarkerManager.getMarker("PARENT_MARKER");
    expected.addParents(parent);
    final String str = writeValueAsString(expected);
    Assert.assertTrue(str.contains("PARENT_MARKER"));
    final Marker actual = reader.readValue(str);
    Assert.assertEquals(expected, actual);
}
 
Example 7
Source File: FlumePersistentAppenderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingle() throws IOException {

    final Logger logger = LogManager.getLogger("EventLogger");
    final Marker marker = MarkerManager.getMarker("EVENT");
    logger.info(marker, "This is a test message");

    final Event event = primary.poll();
    Assert.assertNotNull(event);
    final String body = getBody(event);
    Assert.assertTrue("Channel contained event, but not expected message. Received: " + body,
        body.endsWith("This is a test message"));
}
 
Example 8
Source File: CustomLoggingIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenLoggerWithConsoleConfig_whenFilterByMarker_thanOK() throws Exception {
    Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_MARKER");
    Marker appError = MarkerManager.getMarker("APP_ERROR");
    Marker connectionTrace = MarkerManager.getMarker("CONN_TRACE");

    logger.error(appError, "This marker message at ERROR level should be hidden.");
    logger.trace(connectionTrace, "This is a marker message at TRACE level.");
}
 
Example 9
Source File: MarkerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddSameMarker() {
	final String childMarkerName = CHILD_MAKER_NAME + "-ASM";
	final String parentMakerName = PARENT_MARKER_NAME + "-ASM";
	final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName);
	final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName);
	slf4jMarker.add(slf4jParent);
	slf4jMarker.add(slf4jParent);
	final Marker log4jParent = MarkerManager.getMarker(parentMakerName);
	final Marker log4jMarker = MarkerManager.getMarker(childMarkerName);
	assertTrue(String.format("%s (log4jMarker=%s) is not an instance of %s (log4jParent=%s) in Log4j",
			childMarkerName, parentMakerName, log4jMarker, log4jParent), log4jMarker.isInstanceOf(log4jParent));
	assertTrue(String.format("%s (slf4jMarker=%s) is not an instance of %s (log4jParent=%s) in SLF4J",
			childMarkerName, parentMakerName, slf4jMarker, slf4jParent), slf4jMarker.contains(slf4jParent));
}
 
Example 10
Source File: AbstractMarkerMixInTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNameOnly() throws IOException {
    final Marker expected = MarkerManager.getMarker("A");
    final String str = writeValueAsString(expected);
    Assert.assertFalse(str.contains("parents"));
    final Marker actual = reader.readValue(str);
    Assert.assertEquals(expected, actual);
}
 
Example 11
Source File: MarkerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddSameMarker() {
	final String childMarkerName = CHILD_MAKER_NAME + "-ASM";
	final String parentMakerName = PARENT_MARKER_NAME + "-ASM";
	final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName);
	final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName);
	slf4jMarker.add(slf4jParent);
	slf4jMarker.add(slf4jParent);
	final Marker log4jParent = MarkerManager.getMarker(parentMakerName);
	final Marker log4jMarker = MarkerManager.getMarker(childMarkerName);
	assertTrue(String.format("%s (log4jMarker=%s) is not an instance of %s (log4jParent=%s) in Log4j",
			childMarkerName, parentMakerName, log4jMarker, log4jParent), log4jMarker.isInstanceOf(log4jParent));
	assertTrue(String.format("%s (slf4jMarker=%s) is not an instance of %s (log4jParent=%s) in SLF4J",
			childMarkerName, parentMakerName, slf4jMarker, slf4jParent), slf4jMarker.contains(slf4jParent));
}
 
Example 12
Source File: MarkerAttributeConverter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public Marker convertToEntityAttribute(final String s) {
    if (Strings.isEmpty(s)) {
        return null;
    }

    final int bracket = s.indexOf("[");

    return bracket < 1 ? MarkerManager.getMarker(s) : MarkerManager.getMarker(s.substring(0, bracket));
}
 
Example 13
Source File: MarkerSimpleNamePatternConverterTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 14
Source File: MarkerPatternConverterTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 15
Source File: RingBufferLogEventTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Test
public void testCreateMementoReturnsCopy() {
    final RingBufferLogEvent evt = new RingBufferLogEvent();
    final String loggerName = "logger.name";
    final Marker marker = MarkerManager.getMarker("marked man");
    final String fqcn = "f.q.c.n";
    final Level level = Level.TRACE;
    final Message data = new SimpleMessage("message");
    final Throwable t = new InternalError("not a real error");
    final ContextStack contextStack = new MutableThreadContextStack(Arrays.asList("a", "b"));
    final String threadName = "main";
    final StackTraceElement location = null;
    evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(),
            contextStack, -1, threadName, -1, location, new FixedPreciseClock(12345, 678), new DummyNanoClock(1));
    ((StringMap) evt.getContextData()).putValue("key", "value");

    final LogEvent actual = evt.createMemento();
    assertEquals(evt.getLoggerName(), actual.getLoggerName());
    assertEquals(evt.getMarker(), actual.getMarker());
    assertEquals(evt.getLoggerFqcn(), actual.getLoggerFqcn());
    assertEquals(evt.getLevel(), actual.getLevel());
    assertEquals(evt.getMessage(), actual.getMessage());
    assertEquals(evt.getThrown(), actual.getThrown());
    assertEquals(evt.getContextData(), actual.getContextData());
    assertEquals(evt.getContextStack(), actual.getContextStack());
    assertEquals(evt.getThreadName(), actual.getThreadName());
    assertEquals(evt.getTimeMillis(), actual.getTimeMillis());
    assertEquals(evt.getInstant().getNanoOfMillisecond(), actual.getInstant().getNanoOfMillisecond());
    assertEquals(evt.getSource(), actual.getSource());
    assertEquals(evt.getThrownProxy(), actual.getThrownProxy());
}
 
Example 16
Source File: Log4jMarkerFactory.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a Log4j Marker that is compatible with SLF4J.
 * @param name The name of the Marker.
 * @return A Marker.
 */
@Override
public Marker getMarker(final String name) {
    if (name == null) {
        throw new IllegalArgumentException("Marker name must not be null");
    }
    final Marker marker = markerMap.get(name);
    if (marker != null) {
        return marker;
    }
    final org.apache.logging.log4j.Marker log4jMarker = MarkerManager.getMarker(name);
    return addMarkerIfAbsent(name, log4jMarker);
}
 
Example 17
Source File: LogEventFixtures.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
/**
 * @return a log event that uses all the bells and whistles, features, nooks and crannies
 */
public static Log4jLogEvent createLogEvent() {
    final Marker cMarker = MarkerManager.getMarker("Marker1");
    final Marker pMarker1 = MarkerManager.getMarker("ParentMarker1");
    final Marker pMarker2 = MarkerManager.getMarker("ParentMarker2");
    final Marker gfMarker = MarkerManager.getMarker("GrandFatherMarker");
    final Marker gmMarker = MarkerManager.getMarker("GrandMotherMarker");
    cMarker.addParents(pMarker1);
    cMarker.addParents(pMarker2);
    pMarker1.addParents(gmMarker);
    pMarker1.addParents(gfMarker);
    final Exception sourceHelper = new Exception();
    sourceHelper.fillInStackTrace();
    final Exception cause = new NullPointerException("testNPEx");
    sourceHelper.fillInStackTrace();
    final StackTraceElement source = sourceHelper.getStackTrace()[0];
    final IOException ioException = new IOException("testIOEx", cause);
    ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 1"));
    ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 2"));
    final ThrowableProxy throwableProxy = new ThrowableProxy(ioException);
    final StringMap contextData = ContextDataFactory.createContextData();
    contextData.putValue("MDC.A", "A_Value");
    contextData.putValue("MDC.B", "B_Value");
    final DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true);
    contextStack.clear();
    contextStack.push("stack_msg1");
    contextStack.add("stack_msg2");
    final Log4jLogEvent expected = Log4jLogEvent.newBuilder() //
            .setLoggerName("a.B") //
            .setMarker(cMarker) //
            .setLoggerFqcn("f.q.c.n") //
            .setLevel(Level.DEBUG) //
            .setMessage(new SimpleMessage("Msg")) //
            .setThrown(ioException) //
            .setThrownProxy(throwableProxy) //
            .setContextData(contextData) //
            .setContextStack(contextStack) //
            .setThreadName("MyThreadName") //
            .setSource(source) //
            .setTimeMillis(1).build();
    // validate event?
    return expected;
}
 
Example 18
Source File: GelfAppenderTest.java    From log4j2-gelf with Apache License 2.0 4 votes vote down vote up
@Test
public void testMarker() {
    final Marker parent = MarkerManager.getMarker("PARENT");
    final Marker marker = MarkerManager.getMarker("TEST").addParents(parent);
    logger.info(marker, "Hello World");
}
 
Example 19
Source File: Log4jLogEventTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testBuilderCorrectlyCopiesMutableLogEvent() throws Exception {
    final StringMap contextData = new SortedArrayStringMap();
    contextData.putValue("A", "B");
    final ContextStack contextStack = ThreadContext.getImmutableStack();
    final Exception exception = new Exception("test");
    final Marker marker = MarkerManager.getMarker("EVENTTEST");
    final Message message = new SimpleMessage("foo");
    new StackTraceElement("A", "B", "file", 123);
    final String fqcn = "qualified";
    final String name = "Ceci n'est pas une pipe";
    final String threadName = "threadName";
    final MutableLogEvent event = new MutableLogEvent();
    event.setContextData(contextData);
    event.setContextStack(contextStack);
    event.setEndOfBatch(true);
    event.setIncludeLocation(true);
    //event.setSource(stackTraceElement); // cannot be explicitly set
    event.setLevel(Level.FATAL);
    event.setLoggerFqcn(fqcn);
    event.setLoggerName(name);
    event.setMarker(marker);
    event.setMessage(message);
    event.setNanoTime(1234567890L);
    event.setThreadName(threadName);
    event.setThrown(exception);
    event.setTimeMillis(987654321L);

    assertSame(contextData, event.getContextData());
    assertSame(contextStack, event.getContextStack());
    assertEquals(true, event.isEndOfBatch());
    assertEquals(true, event.isIncludeLocation());
    assertSame(Level.FATAL, event.getLevel());
    assertSame(fqcn, event.getLoggerFqcn());
    assertSame(name, event.getLoggerName());
    assertSame(marker, event.getMarker());
    assertSame(message, event.getMessage());
    assertEquals(1234567890L, event.getNanoTime());
    //assertSame(stackTraceElement, event.getSource()); // don't invoke
    assertSame(threadName, event.getThreadName());
    assertSame(exception, event.getThrown());
    assertEquals(987654321L, event.getTimeMillis());

    final LogEvent e2 = new Log4jLogEvent.Builder(event).build();
    assertEquals(contextData, e2.getContextData());
    assertSame(contextStack, e2.getContextStack());
    assertEquals(true, e2.isEndOfBatch());
    assertEquals(true, e2.isIncludeLocation());
    assertSame(Level.FATAL, e2.getLevel());
    assertSame(fqcn, e2.getLoggerFqcn());
    assertSame(name, e2.getLoggerName());
    assertSame(marker, e2.getMarker());
    assertSame(message, e2.getMessage());
    assertEquals(1234567890L, e2.getNanoTime());
    //assertSame(stackTraceElement, e2.getSource()); // don't invoke
    assertSame(threadName, e2.getThreadName());
    assertSame(exception, e2.getThrown());
    assertEquals(987654321L, e2.getTimeMillis());

    // use reflection to get value of source field in log event copy:
    // invoking the getSource() method would initialize the field
    final Field fieldSource = Log4jLogEvent.class.getDeclaredField("source");
    fieldSource.setAccessible(true);
    final Object value = fieldSource.get(e2);
    assertNull("source in copy", value);
}
 
Example 20
Source File: Log4J2Logger.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** Returns Marker object for the specified name, or null if the name is null */
private Marker getMarkerOrNull(@Nullable String marker) {
    return marker != null ? MarkerManager.getMarker(marker) : null;
}