Java Code Examples for org.apache.logging.log4j.core.impl.MutableLogEvent#setMessage()

The following examples show how to use org.apache.logging.log4j.core.impl.MutableLogEvent#setMessage() . 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: 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 3
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));
}