Search in sources :

Example 6 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)

Example 7 with MutableLogEvent

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);
        }
    }
}
Also used : Message(org.apache.logging.log4j.message.Message) StringWriter(java.io.StringWriter) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent) MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent) Date(java.util.Date) PrintWriter(java.io.PrintWriter)

Example 8 with MutableLogEvent

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

Example 9 with MutableLogEvent

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

Example 10 with MutableLogEvent

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);
    }
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) Log4jJsonObjectMapper(org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper) MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

MutableLogEvent (org.apache.logging.log4j.core.impl.MutableLogEvent)11 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)6 Test (org.junit.Test)5 AbstractJacksonLayout (org.apache.logging.log4j.jackson.AbstractJacksonLayout)4 Message (org.apache.logging.log4j.message.Message)4 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)3 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)2 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