org.apache.logging.log4j.core.impl.MutableLogEvent Java Examples

The following examples show how to use org.apache.logging.log4j.core.impl.MutableLogEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IbisMaskingLayout.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Override
public final String toSerializable(LogEvent logEvent) {
	MutableLogEvent event = convertToMutableLog4jEvent(logEvent);
	Message msg = event.getMessage();
	String message = msg.getFormattedMessage();

	if (StringUtils.isNotEmpty(message)) {
		message = Misc.hideAll(message, globalReplace);
		message = Misc.hideAll(message, threadLocalReplace.get());

		int length = message.length();
		if (maxLength > 0 && length > maxLength) {
			int diff = length - maxLength;
			//We trim the message because it may end with a newline or whitespace character.
			message = message.substring(0, maxLength).trim() + " " + String.format(moreMessage, diff) + "\r\n";
		}
	}

	event.setMessage(new LogMessage(message, msg.getThrowable()));

	return serializeEvent(event);
}
 
Example #2
Source File: YamlLayoutTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: AsyncLoggerConfigDisruptor.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private LogEvent prepareEvent(final LogEvent event) {
    LogEvent logEvent = ensureImmutable(event);
    if (logEvent.getMessage() instanceof ReusableMessage) {
        if (logEvent instanceof Log4jLogEvent) {
            ((Log4jLogEvent) logEvent).makeMessageImmutable();
        } else if (logEvent instanceof MutableLogEvent) {
            // MutableLogEvents need to be translated into the RingBuffer by the MUTABLE_TRANSLATOR.
            // That translator calls MutableLogEvent.initFrom to copy the event, which will makeMessageImmutable the message.
            if (translator != MUTABLE_TRANSLATOR) { // should not happen...
                // TRANSLATOR expects an immutable LogEvent
                logEvent = ((MutableLogEvent) logEvent).createMemento();
            }
        } else { // custom log event, with a ReusableMessage
            showWarningAboutCustomLogEventWithReusableMessage(logEvent);
        }
    } else { // message is not a ReusableMessage; makeMessageImmutable it to prevent ConcurrentModificationExceptions
        InternalAsyncUtil.makeMessageImmutable(logEvent.getMessage()); // LOG4J2-1988, LOG4J2-1914
    }
    return logEvent;
}
 
Example #4
Source File: ListAppender.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Override
public void append(final LogEvent event) {
    final Layout<? extends Serializable> layout = getLayout();
    if (layout == null) {
        if (event instanceof MutableLogEvent) {
            // must take snapshot or subsequent calls to logger.log() will modify this event
            events.add(((MutableLogEvent) event).createMemento());
        } else {
            events.add(event);
        }
    } else {
        write(layout.toByteArray(event));
    }
    if (countDownLatch != null) {
        countDownLatch.countDown();
    }
}
 
Example #5
Source File: JsonLayoutTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #6
Source File: IbisXmlLayoutTest.java    From iaf with Apache License 2.0 5 votes vote down vote up
private LogEvent generateLogEvent(String string) {
	MutableLogEvent logEvent = new MutableLogEvent();
	logEvent.setLevel(Level.DEBUG);
	Message message = new SimpleMessage(string);
	logEvent.setMessage(message);
	return logEvent;
}
 
Example #7
Source File: SmtpManager.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public void add(LogEvent event) {
    if (event instanceof Log4jLogEvent && event.getMessage() instanceof ReusableMessage) {
        ((Log4jLogEvent) event).makeMessageImmutable();
    } else if (event instanceof MutableLogEvent) {
        event = ((MutableLogEvent) event).createMemento();
    }
    buffer.add(event);
}
 
Example #8
Source File: XmlLayoutTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #9
Source File: AsyncLoggerConfigDisruptor.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Release references held by ring buffer to allow objects to be garbage-collected.
 */
public void clear() {
    loggerConfig = null;
    if (event instanceof MutableLogEvent) {
        ((MutableLogEvent) event).clear();
    } else {
        event = null;
    }
}
 
Example #10
Source File: MutableLogEventWithReusableParamMsgTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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();

    Message mementoMessage = evt.memento();
    Message mementoMessageSecondInvocation = evt.memento();
    // MutableLogEvent.memento should be cached
    assertThat(mementoMessage, sameInstance(mementoMessageSecondInvocation));
}
 
Example #11
Source File: JsonLayoutTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #12
Source File: AbstractJacksonLayout.java    From summerframework with Apache License 2.0 4 votes vote down vote up
private static LogEvent convertMutableToLog4jEvent(final LogEvent event) {
    return event instanceof MutableLogEvent ? ((MutableLogEvent)event).createMemento() : event;
}
 
Example #13
Source File: IbisMaskingLayout.java    From iaf with Apache License 2.0 4 votes vote down vote up
private MutableLogEvent convertToMutableLog4jEvent(final LogEvent event) {
//		LogEvent e = (event instanceof Log4jLogEvent ? event : Log4jLogEvent.createMemento(event));
		MutableLogEvent mutable = new MutableLogEvent();
		mutable.initFrom(event);
		return mutable;
	}
 
Example #14
Source File: AsyncLoggerConfigDisruptor.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public Log4jEventWrapper(final MutableLogEvent mutableLogEvent) {
    event = mutableLogEvent;
}
 
Example #15
Source File: AsyncLoggerConfigDisruptor.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public Log4jEventWrapper newInstance() {
    return new Log4jEventWrapper(new MutableLogEvent());
}
 
Example #16
Source File: AsyncLoggerConfigDisruptor.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public void translateTo(final Log4jEventWrapper ringBufferElement, final long sequence,
        final LogEvent logEvent, final AsyncLoggerConfig loggerConfig) {
    ((MutableLogEvent) ringBufferElement.event).initFrom(logEvent);
    ringBufferElement.loggerConfig = loggerConfig;
}