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();
}
use of org.apache.logging.log4j.core.impl.MutableLogEvent in project VoxelGamesLibv2 by VoxelGamesLib.
the class LogFormatter method log.
public void log(long millies, String levelName, String loggerName, String message, @Nullable Throwable throwable) {
levelName = formatLevel(levelName, message);
loggerName = formatLoggerName(loggerName, message);
message = formatMessage(message);
// print out to sout
StringBuilder sb = new StringBuilder();
sb.append("[").append(hourFormat.format(new Date(millies))).append(" ").append(levelName).append("]: ");
sb.append("<").append(loggerName).append("> ");
sb.append(message);
sout.println(sb.toString());
// print to log4j to handle file stuff
if (log4jAppender != null) {
Message log4jMessage = new MutableLogEvent(new StringBuilder(message), new Object[0]);
LogEvent log4jEvent = Log4jLogEvent.newBuilder().setMessage(log4jMessage).setTimeMillis(millies).setLevel(toLog4j(levelName)).setLoggerName(loggerName).build();
log4jAppender.append(log4jEvent);
}
if (throwable != null) {
// print to sout
throwable.printStackTrace(sout);
// print to log4j to handle file stuff
if (log4jAppender != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
throwable.printStackTrace(pw);
Message log4jMessage = new MutableLogEvent(new StringBuilder(sw.toString()), new Object[0]);
LogEvent log4jEvent = Log4jLogEvent.newBuilder().setMessage(log4jMessage).setTimeMillis(millies).setLevel(toLog4j(levelName)).setLoggerName(loggerName).build();
log4jAppender.append(log4jEvent);
}
}
}
use of org.apache.logging.log4j.core.impl.MutableLogEvent in project iaf by ibissource.
the class IbisMaskingLayout method updateLogEventMessage.
/**
* When converting from a (Log4jLogEvent) to a mutable LogEvent ensure to not invoke any getters but assign the fields directly.
* @see "https://issues.apache.org/jira/browse/LOG4J2-1179"
* @see "https://issues.apache.org/jira/browse/LOG4J2-1382"
*
* Directly calling RewriteAppender.append(LogEvent) can do 44 million ops/sec, but when calling rewriteLogger.debug(msg) to invoke
* a logger that calls this appender, all of a sudden throughput drops to 37 thousand ops/sec. That's 1000x slower.
*
* Rewriting the event ({@link MutableLogEvent#initFrom(LogEvent)}) includes invoking caller location information, {@link LogEvent#getSource()}
* This is done by taking a snapshot of the stack and walking it, @see {@link StackLocatorUtil#calcLocation(String)}).
* Hence avoid this at all costs, fixed from version 2.6 (LOG4J2-1382) use a builder instance to update the @{link Message}.
*/
private LogEvent updateLogEventMessage(LogEvent event, Message message) {
if (event instanceof Log4jLogEvent) {
Log4jLogEvent.Builder builder = ((Log4jLogEvent) event).asBuilder();
builder.setMessage(message);
return builder.build();
}
// NB: this might trigger a source location lookup.
MutableLogEvent mutable = new MutableLogEvent();
mutable.initFrom(event);
mutable.setMessage(message);
return mutable;
}
use of org.apache.logging.log4j.core.impl.MutableLogEvent in project logging-log4j2 by apache.
the class SmtpManagerTest method testAdd_WhereMutableLogEvent.
// LOG4J2-3172: make sure existing protections are not violated
@Test
void testAdd_WhereMutableLogEvent() {
MutableLogEvent event = new MutableLogEvent(new StringBuilder("test message"), null);
testAdd(event);
}
use of org.apache.logging.log4j.core.impl.MutableLogEvent in project logging-log4j2 by apache.
the class JsonLayoutTest method testReusableLayoutMessageWithCurlyBraces.
// Test for LOG4J2-2345
@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);
}
}
Aggregations