use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.
the class YamlLayoutTest method testMutableLogEvent.
@Test
public void testMutableLogEvent() throws Exception {
final AbstractJacksonLayout layout = YamlLayout.newBuilder().setLocationInfo(false).setProperties(false).setIncludeStacktrace(false).setAdditionalFields(new KeyValuePair[] { new KeyValuePair("KEY1", "VALUE1"), new KeyValuePair("KEY2", "${java:runtime}") }).setCharset(StandardCharsets.UTF_8).setConfiguration(ctx.getConfiguration()).build();
Log4jLogEvent logEvent = LogEventFixtures.createLogEvent();
final MutableLogEvent mutableEvent = new MutableLogEvent();
mutableEvent.initFrom(logEvent);
final String strLogEvent = layout.toSerializable(logEvent);
final String strMutableEvent = layout.toSerializable(mutableEvent);
assertEquals(strMutableEvent, strLogEvent, strMutableEvent);
}
use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.
the class XmlLayoutTest method testAllFeatures.
/**
* @param includeLocationInfo
* TODO
* @param compact
* @param includeContextMap
* TODO
* @param includeContextStack
* TODO
* @throws IOException
* @throws JsonParseException
* @throws JsonMappingException
*/
private void testAllFeatures(final boolean includeLocationInfo, final boolean compact, final boolean includeContextMap, final boolean includeContextStack, final boolean includeStacktrace) throws IOException, JsonParseException, JsonMappingException {
final Log4jLogEvent expected = LogEventFixtures.createLogEvent();
// @formatter:off
final XmlLayout layout = XmlLayout.newBuilder().setLocationInfo(includeLocationInfo).setProperties(includeContextMap).setComplete(false).setCompact(compact).setIncludeStacktrace(includeStacktrace).setCharset(StandardCharsets.UTF_8).build();
final String str = layout.toSerializable(expected);
// @formatter:on
// System.out.println(str);
assertEquals(str, !compact, str.contains("\n"));
assertEquals(str, includeLocationInfo, str.contains("Source"));
assertEquals(str, includeContextMap, str.contains("ContextMap"));
final Log4jLogEvent actual = new Log4jXmlObjectMapper().readValue(str, Log4jLogEvent.class);
LogEventFixtures.assertEqualLogEvents(expected, actual, includeLocationInfo, includeContextMap, includeStacktrace);
if (includeContextMap) {
this.checkContextMapElement("MDC.A", "A_Value", compact, str);
this.checkContextMapElement("MDC.B", "B_Value", compact, str);
}
if (includeContextStack) {
this.checkContextStackElement("stack_msg1", compact, str);
this.checkContextStackElement("stack_msg2", compact, str);
}
//
assertNull(actual.getThrown());
// check some attrs
assertTrue(str, str.contains("loggerFqcn=\"f.q.c.n\""));
assertTrue(str, str.contains("loggerName=\"a.B\""));
// make sure short names are used
assertTrue(str, str.contains("<Event "));
if (includeStacktrace) {
assertTrue("Missing \"class=\" in: " + str, str.contains("class="));
assertTrue("Missing \"method=\" in: " + str, str.contains("method="));
assertTrue("Missing \"file=\" in: " + str, str.contains("file="));
assertTrue("Missing \"line=\" in: " + str, str.contains("line="));
}
//
// make sure the names we want are used
// this.checkAttributeName("timeMillis", compact, str);
this.checkElementName("Instant", compact, str, true, false);
this.checkAttributeName("epochSecond", compact, str);
this.checkAttributeName("nanoOfSecond", compact, str);
// and not threadName
this.checkAttributeName("thread", compact, str);
this.checkAttributeName("level", compact, str);
this.checkAttributeName("loggerName", compact, str);
this.checkElementName("Marker", compact, str, true, true);
this.checkAttributeName("name", compact, str);
this.checkElementName("Parents", compact, str, false, true);
this.checkElementName("Message", compact, str, false, true);
this.checkElementName("Thrown", compact, str, true, true);
this.checkElementName("Cause", compact, str, true, includeStacktrace);
this.checkAttributeName("commonElementCount", compact, str);
this.checkAttributeName("message", compact, str);
this.checkAttributeName("localizedMessage", compact, str);
if (includeStacktrace) {
this.checkElementName("ExtendedStackTrace", compact, str, false, true);
this.checkAttributeName("class", compact, str);
this.checkAttributeName("method", compact, str);
this.checkAttributeName("file", compact, str);
this.checkAttributeName("line", compact, str);
this.checkAttributeName("exact", compact, str);
this.checkAttributeName("location", compact, str);
this.checkAttributeName("version", compact, str);
} else {
this.checkElementNameAbsent("ExtendedStackTrace", compact, str);
}
this.checkElementName("Suppressed", compact, str, false, true);
this.checkAttributeName("loggerFqcn", compact, str);
this.checkAttributeName("endOfBatch", compact, str);
if (includeContextMap) {
this.checkElementName("ContextMap", compact, str, false, true);
} else {
this.checkElementNameAbsent("ContextMap", compact, str);
}
this.checkElementName("ContextStack", compact, str, false, true);
if (includeLocationInfo) {
this.checkElementName("Source", compact, str, true, false);
} else {
this.checkElementNameAbsent("Source", compact, str);
}
// check some attrs
this.checkAttribute("loggerFqcn", "f.q.c.n", compact, str);
this.checkAttribute("loggerName", "a.B", compact, str);
this.checkJsonPropertyOrder(includeContextStack, includeContextMap, includeStacktrace, str);
}
use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.
the class XmlLayoutTest method prepareXMLForStacktraceTests.
private String prepareXMLForStacktraceTests(final boolean stacktraceAsString) {
final Log4jLogEvent expected = LogEventFixtures.createLogEvent();
// @formatter:off
final AbstractJacksonLayout layout = XmlLayout.newBuilder().setCompact(true).setIncludeStacktrace(true).setStacktraceAsString(stacktraceAsString).build();
// @formatter:off
return layout.toSerializable(expected);
}
use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.
the class XmlLayoutTest method testLayoutLoggerName.
@Test
public void testLayoutLoggerName() {
final XmlLayout layout = XmlLayout.newBuilder().setLocationInfo(false).setProperties(true).setComplete(true).setCompact(false).setIncludeStacktrace(true).build();
final Log4jLogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
"a.B").setLoggerFqcn(//
"f.q.c.n").setLevel(//
Level.DEBUG).setMessage(//
new SimpleMessage("M")).setThreadName(//
"threadName").setTimeMillis(1).build();
final String str = layout.toSerializable(event);
assertTrue(str, str.contains("loggerName=\"a.B\""));
}
use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.
the class AsyncAppender method append.
/**
* Actual writing occurs here.
*
* @param logEvent The LogEvent.
*/
@Override
public void append(final LogEvent logEvent) {
if (!isStarted()) {
throw new IllegalStateException("AsyncAppender " + getName() + " is not active");
}
final Log4jLogEvent memento = Log4jLogEvent.createMemento(logEvent, includeLocation);
InternalAsyncUtil.makeMessageImmutable(logEvent.getMessage());
if (!transfer(memento)) {
if (blocking) {
if (AbstractLogger.getRecursionDepth() > 1) {
// LOG4J2-1518, LOG4J2-2031
// If queue is full AND we are in a recursive call, call appender directly to prevent deadlock
AsyncQueueFullMessageUtil.logWarningToStatusLogger();
logMessageInCurrentThread(logEvent);
} else {
// delegate to the event router (which may discard, enqueue and block, or log in current thread)
final EventRoute route = asyncQueueFullPolicy.getRoute(dispatcher.getId(), memento.getLevel());
route.logMessage(this, memento);
}
} else {
error("Appender " + getName() + " is unable to write primary appenders. queue is full");
logToErrorAppenderIfNecessary(false, memento);
}
}
}
Aggregations