use of org.apache.logging.log4j.core.impl.MutableLogEvent in project logging-log4j2 by apache.
the class JsonLayoutTest method testMutableLogEvent.
@Test
public void testMutableLogEvent() throws Exception {
final AbstractJacksonLayout layout = JsonLayout.newBuilder().setLocationInfo(false).setProperties(false).setComplete(false).setCompact(true).setEventEol(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.MutableLogEvent in project logging-log4j2 by apache.
the class XmlLayoutTest method testMutableLogEvent.
@Test
public void testMutableLogEvent() throws Exception {
final AbstractJacksonLayout layout = XmlLayout.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.MutableLogEvent in project logging-log4j2 by apache.
the class MutableLogEventWithReusableParamMsgTest method testInteractionWithReusableParameterizedMessage.
@Test
public void testInteractionWithReusableParameterizedMessage() {
final MutableLogEvent evt = new MutableLogEvent();
final MutableMessage msg = new MutableMessage();
msg.set("Hello {} {} {}", 1, 2, 3);
evt.setMessage(msg);
evt.clear();
msg.set("Hello {}", new Object[] { 1 });
evt.setMessage(msg);
evt.clear();
msg.set("Hello {}", 1);
evt.setMessage(msg);
evt.clear();
// Uncomment out this log event and the params gets reset correctly (No exception occurs)
// msg.set("Hello {}", 1);
// evt.setMessage(msg);
// evt.clear();
// Exception at this log event - as the params is set to 1!
msg.set("Hello {} {} {}", 1, 2, 3);
evt.setMessage(msg);
evt.clear();
Message mementoMessage = evt.memento();
Message mementoMessageSecondInvocation = evt.memento();
// MutableLogEvent.memento should be cached
assertThat(mementoMessage, sameInstance(mementoMessageSecondInvocation));
}
use of org.apache.logging.log4j.core.impl.MutableLogEvent in project jabref by JabRef.
the class LogMessages method add.
public void add(LogEvent event) {
// We need to make a copy as instances of LogEvent are reused by log4j
MutableLogEvent copy = new MutableLogEvent();
copy.initFrom(event);
messages.add(copy);
}
use of org.apache.logging.log4j.core.impl.MutableLogEvent in project logging-log4j2 by apache.
the class MutableLogEventWithReusableParamMsgTest method testInteractionWithReusableParameterizedMessage.
@Test
public void testInteractionWithReusableParameterizedMessage() {
final MutableLogEvent evt = new MutableLogEvent();
final ReusableParameterizedMessage msg = new ReusableParameterizedMessage();
msg.set("Hello {} {} {}", 1, 2, 3);
evt.setMessage(msg);
evt.clear();
msg.set("Hello {}", new Object[] { 1 });
evt.setMessage(msg);
evt.clear();
msg.set("Hello {}", 1);
evt.setMessage(msg);
evt.clear();
// Uncomment out this log event and the params gets reset correctly (No exception occurs)
// msg.set("Hello {}", 1);
// evt.setMessage(msg);
// evt.clear();
// Exception at this log event - as the params is set to 1!
msg.set("Hello {} {} {}", 1, 2, 3);
evt.setMessage(msg);
evt.clear();
}
Aggregations