use of org.apache.logging.log4j.core.impl.Log4jLogEvent 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
*/
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;
}
use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.
the class PatternParserTest method testDeeplyNestedPattern.
@Test
public void testDeeplyNestedPattern() {
final List<PatternFormatter> formatters = parser.parse(deeplyNestedPattern);
assertNotNull(formatters);
assertEquals(1, formatters.size());
final StringMap mdc = ContextDataFactory.createContextData();
mdc.putValue("var", "1234");
final Log4jLogEvent event = //
Log4jLogEvent.newBuilder().setContextData(mdc).build();
final StringBuilder buf = new StringBuilder();
formatters.get(0).format(event, buf);
final String expected = " 123 ";
assertEquals(expected, buf.toString());
}
use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.
the class JsonLayoutTest method testReusableLayoutMessageWithCurlyBraces.
// Test for LOG4J2-2345
@Test
public void testReusableLayoutMessageWithCurlyBraces() throws Exception {
final boolean propertiesAsList = false;
final AbstractJacksonLayout layout = JsonLayout.newBuilder().setLocationInfo(false).setProperties(false).setPropertiesAsList(propertiesAsList).setComplete(false).setCompact(true).setEventEol(false).setCharset(StandardCharsets.UTF_8).setIncludeStacktrace(true).build();
Message message = ReusableMessageFactory.INSTANCE.newMessage("Testing {}", new TestObj());
try {
final Log4jLogEvent expected = Log4jLogEvent.newBuilder().setLoggerName("a.B").setLoggerFqcn("f.q.c.n").setLevel(Level.DEBUG).setMessage(message).setThreadName("threadName").setTimeMillis(1).build();
MutableLogEvent mutableLogEvent = new MutableLogEvent();
mutableLogEvent.initFrom(expected);
final String str = layout.toSerializable(mutableLogEvent);
final String expectedMessage = "Testing " + TestObj.TO_STRING_VALUE;
assertTrue(str, str.contains("\"message\":\"" + expectedMessage + '"'));
final Log4jLogEvent actual = new Log4jJsonObjectMapper(propertiesAsList, true, false, false).readValue(str, Log4jLogEvent.class);
assertEquals(expectedMessage, actual.getMessage().getFormattedMessage());
} finally {
ReusableMessageFactory.release(message);
}
}
use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.
the class JsonLayoutTest method testLayoutRingBufferEventReusableMessageWithCurlyBraces.
// Test for LOG4J2-2312 LOG4J2-2341
@Test
public void testLayoutRingBufferEventReusableMessageWithCurlyBraces() throws Exception {
final boolean propertiesAsList = false;
final AbstractJacksonLayout layout = JsonLayout.newBuilder().setLocationInfo(false).setProperties(false).setPropertiesAsList(propertiesAsList).setComplete(false).setCompact(true).setEventEol(false).setCharset(StandardCharsets.UTF_8).setIncludeStacktrace(true).build();
Message message = ReusableMessageFactory.INSTANCE.newMessage("Testing {}", new TestObj());
try {
RingBufferLogEvent ringBufferEvent = new RingBufferLogEvent();
ringBufferEvent.setValues(null, "a.B", null, "f.q.c.n", Level.DEBUG, message, null, new SortedArrayStringMap(), ThreadContext.EMPTY_STACK, 1L, "threadName", 1, null, new SystemClock(), new DummyNanoClock());
final String str = layout.toSerializable(ringBufferEvent);
final String expectedMessage = "Testing " + TestObj.TO_STRING_VALUE;
assertThat(str, containsString("\"message\":\"" + expectedMessage + '"'));
final Log4jLogEvent actual = new Log4jJsonObjectMapper(propertiesAsList, true, false, false).readValue(str, Log4jLogEvent.class);
assertEquals(expectedMessage, actual.getMessage().getFormattedMessage());
} finally {
ReusableMessageFactory.release(message);
}
}
use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.
the class JsonLayoutTest method testLayoutLoggerName.
@Test
public void testLayoutLoggerName() throws Exception {
final boolean propertiesAsList = false;
// @formatter:off
final AbstractJacksonLayout layout = JsonLayout.newBuilder().setLocationInfo(false).setProperties(false).setPropertiesAsList(propertiesAsList).setComplete(false).setCompact(true).setEventEol(false).setCharset(StandardCharsets.UTF_8).setIncludeStacktrace(true).build();
// @formatter:on
// @formatter:off
final Log4jLogEvent expected = Log4jLogEvent.newBuilder().setLoggerName("a.B").setLoggerFqcn("f.q.c.n").setLevel(Level.DEBUG).setMessage(new SimpleMessage("M")).setThreadName("threadName").setTimeMillis(1).build();
// @formatter:on
final String str = layout.toSerializable(expected);
assertTrue(str, str.contains("\"loggerName\":\"a.B\""));
final Log4jLogEvent actual = new Log4jJsonObjectMapper(propertiesAsList, true, false, false).readValue(str, Log4jLogEvent.class);
assertEquals(expected.getLoggerName(), actual.getLoggerName());
assertEquals(expected, actual);
}
Aggregations