Search in sources :

Example 91 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class MarkerLookupTest method testLookupEventNonExistant.

@Test
public void testLookupEventNonExistant() {
    final LogEvent event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    this.getClass().getName()).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);
    assertNull(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) Test(org.junit.Test)

Example 92 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage 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 93 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage 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)

Example 94 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class LogEventFixtures method createLogEvent.

/**
     * @return a log event that uses all the bells and whistles, features, nooks and crannies
     */
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 Map<String, String> contextMap = new HashMap<>();
    contextMap.put("MDC.A", "A_Value");
    contextMap.put("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).setContextMap(//
    contextMap).setContextStack(//
    contextStack).setThreadName(//
    "MyThreadName").setSource(//
    source).setTimeMillis(1).build();
    // validate event?
    return expected;
}
Also used : HashMap(java.util.HashMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) IOException(java.io.IOException) ThrowableProxy(org.apache.logging.log4j.core.impl.ThrowableProxy) IOException(java.io.IOException) DefaultThreadContextStack(org.apache.logging.log4j.spi.DefaultThreadContextStack) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent)

Example 95 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class PatternLayoutTest method testEqualsEmptyMarker.

@Test
public void testEqualsEmptyMarker() throws Exception {
    // replace "[]" with the empty string
    final PatternLayout layout = PatternLayout.newBuilder().withPattern("[%logger]%equals{[%marker]}{[]}{} %msg").withConfiguration(ctx.getConfiguration()).build();
    // Not empty marker
    final LogEvent event1 = //
    Log4jLogEvent.newBuilder().setLoggerName(this.getClass().getName()).setLoggerFqcn(//
    "org.apache.logging.log4j.core.Logger").setLevel(//
    Level.INFO).setMarker(//
    MarkerManager.getMarker("TestMarker")).setMessage(new SimpleMessage("Hello, world!")).build();
    assertToByteArray("[org.apache.logging.log4j.core.layout.PatternLayoutTest][TestMarker] Hello, world!", layout, event1);
    assertEncode("[org.apache.logging.log4j.core.layout.PatternLayoutTest][TestMarker] Hello, world!", layout, event1);
    // empty marker
    final LogEvent event2 = //
    Log4jLogEvent.newBuilder().setLoggerName(this.getClass().getName()).setLoggerFqcn(//
    "org.apache.logging.log4j.core.Logger").setLevel(//
    Level.INFO).setMessage(new SimpleMessage("Hello, world!")).build();
    assertToByteArray("[org.apache.logging.log4j.core.layout.PatternLayoutTest] Hello, world!", layout, event2);
    assertEncode("[org.apache.logging.log4j.core.layout.PatternLayoutTest] Hello, world!", layout, event2);
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Aggregations

SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)130 Test (org.junit.Test)98 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)96 LogEvent (org.apache.logging.log4j.core.LogEvent)93 Message (org.apache.logging.log4j.message.Message)34 Marker (org.apache.logging.log4j.Marker)19 Level (org.apache.logging.log4j.Level)11 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)10 IOException (java.io.IOException)7 LoggerContext (org.apache.logging.log4j.core.LoggerContext)7 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)6 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)6 HashMap (java.util.HashMap)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectInputStream (java.io.ObjectInputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)4