Search in sources :

Example 1 with MutableLogEvent

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);
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 2 with MutableLogEvent

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);
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent) Test(org.junit.Test)

Example 3 with MutableLogEvent

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));
}
Also used : Message(org.apache.logging.log4j.message.Message) ReusableParameterizedMessage(org.apache.logging.log4j.message.ReusableParameterizedMessage) MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent) Test(org.junit.jupiter.api.Test)

Example 4 with MutableLogEvent

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);
}
Also used : MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent)

Example 5 with MutableLogEvent

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();
}
Also used : MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent) Test(org.junit.Test)

Aggregations

MutableLogEvent (org.apache.logging.log4j.core.impl.MutableLogEvent)9 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)5 Test (org.junit.Test)5 AbstractJacksonLayout (org.apache.logging.log4j.jackson.AbstractJacksonLayout)4 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)3 Message (org.apache.logging.log4j.message.Message)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Test (org.junit.jupiter.api.Test)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 LogEvent (org.apache.logging.log4j.core.LogEvent)1 Log4jJsonObjectMapper (org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper)1 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 ReusableParameterizedMessage (org.apache.logging.log4j.message.ReusableParameterizedMessage)1 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)1