Java 类org.apache.logging.log4j.core.impl.MutableLogEvent 实例源码

项目:log4j2-extended-jsonlayout    文件:ListAppender.java   
@Override
public synchronized 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 if (layout instanceof SerializedLayout) {
        final byte[] header = layout.getHeader();
        final byte[] content = layout.toByteArray(event);
        final byte[] record = new byte[header.length + content.length];
        System.arraycopy(header, 0, record, 0, header.length);
        System.arraycopy(content, 0, record, header.length, content.length);
        data.add(record);
    } else {
        write(layout.toByteArray(event));
    }
    if (countDownLatch != null) {
        countDownLatch.countDown();
    }
}
项目:graylog-plugin-internal-logs    文件:SerializedLogEventTransportTest.java   
@Test
public void launchCreatesAndStartsAppenderAndProcessesMessages() throws Exception {
    final MessageInput messageInput = mock(MessageInput.class);
    transport.launch(messageInput);

    final DirectConsumingAppender appender = transport.getAppender();
    assertThat(appender.getName()).isEqualTo("graylog-plugin-internal-logs");
    assertThat(appender.getLayout()).isInstanceOf(SerializedLayout.class);
    assertThat(appender.isStarted()).isTrue();

    final MutableLogEvent logEvent = new MutableLogEvent();
    logEvent.setMessage(new SimpleMessage("Processed"));
    logEvent.setLevel(Level.ERROR);
    appender.append(logEvent);
    verify(messageInput, times(1)).processRawMessage(any(RawMessage.class));

    final MutableLogEvent ignoredLogEvent = new MutableLogEvent();
    ignoredLogEvent.setMessage(new SimpleMessage("Ignored"));
    ignoredLogEvent.setLevel(Level.TRACE);
    appender.append(ignoredLogEvent);
    verifyNoMoreInteractions(messageInput);
}
项目:graylog-plugin-internal-logs    文件:SerializedLogEventCodecTest.java   
private LogEvent createLogEvent(DateTime timestamp) {
    final MutableLogEvent logEvent = new MutableLogEvent();

    logEvent.setMessage(new SimpleMessage("Test"));
    logEvent.setLevel(Level.TRACE);
    logEvent.setLoggerName("org.example.Test");
    logEvent.setMarker(MarkerManager.getMarker("TestMarker"));
    logEvent.setTimeMillis(timestamp.getMillis());
    logEvent.setNanoTime(42L);

    logEvent.setContextStack(new MutableThreadContextStack(ImmutableList.of("one", "two")));
    final SortedArrayStringMap contextData = new SortedArrayStringMap(1);
    contextData.putValue("foobar", "quux");
    logEvent.setContextData(contextData);

    logEvent.setThreadId(23L);
    logEvent.setThreadName("thread-name");
    logEvent.setThreadPriority(42);

    logEvent.setThrown(new Throwable("Test", new Throwable("cause")));

    logEvent.setIncludeLocation(true);

    return logEvent.createMemento();
}
项目:logging-log4j2    文件:AsyncLoggerConfigDisruptor.java   
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;
}
项目:logging-log4j2    文件:MutableLogEventWithReusableParamMsgTest.java   
@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();
}
项目:logging-log4j2    文件:ListAppender.java   
@Override
public synchronized 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 if (layout instanceof SerializedLayout) {
        final byte[] header = layout.getHeader();
        final byte[] content = layout.toByteArray(event);
        final byte[] record = new byte[header.length + content.length];
        System.arraycopy(header, 0, record, 0, header.length);
        System.arraycopy(content, 0, record, header.length, content.length);
        data.add(record);
    } else {
        write(layout.toByteArray(event));
    }
    if (countDownLatch != null) {
        countDownLatch.countDown();
    }
}
项目:log4j2-extended-jsonlayout    文件:ExtendedJsonLayout.java   
private static LogEvent convertMutableToLog4jEvent(final LogEvent event) {
    // TODO Jackson-based layouts have certain filters set up for Log4jLogEvent.
    // TODO Need to set up the same filters for MutableLogEvent but don't know how...
    // This is a workaround.
    return event instanceof MutableLogEvent
            ? ((MutableLogEvent) event).createMemento()
            : event;
}
项目:graylog-plugin-internal-logs    文件:DirectConsumingAppenderTest.java   
@Test
public void appenderSendsLogEventToConsumerIfThresholdMatches() throws Exception {
    final MutableLogEvent logEvent = new MutableLogEvent();
    logEvent.setMessage(new SimpleMessage("Test"));
    logEvent.setLevel(Level.ERROR);
    appender.append(logEvent);

    assertThat(consumer.getProcessedLogEvents()).hasSize(1);
}
项目:graylog-plugin-internal-logs    文件:DirectConsumingAppenderTest.java   
@Test
public void appenderIgnoresLogEventIfThresholdMismatches() throws Exception {
    final MutableLogEvent logEvent = new MutableLogEvent();
    logEvent.setMessage(new SimpleMessage("Test"));
    logEvent.setLevel(Level.TRACE);
    appender.append(logEvent);

    assertThat(consumer.getProcessedLogEvents()).isEmpty();
}
项目:logging-log4j2    文件:SmtpManager.java   
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);
}
项目:logging-log4j2    文件:AsyncLoggerConfigDisruptor.java   
/**
 * 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;
    }
}
项目:logging-log4j2    文件:AbstractJacksonLayout.java   
private static LogEvent convertMutableToLog4jEvent(final LogEvent event) {
    // TODO Jackson-based layouts have certain filters set up for Log4jLogEvent.
    // TODO Need to set up the same filters for MutableLogEvent but don't know how...
    // This is a workaround.
    return event instanceof MutableLogEvent
            ? ((MutableLogEvent) event).createMemento()
            : event;
}
项目:log4j2-simplejson    文件:SimpleJSONLayout.java   
private static LogEvent convertMutableToLog4jEvent(final LogEvent event) {
    // TODO (from JsonLayout): Need to set up the same filters for MutableLogEvent but don't know how...
    return ((event instanceof MutableLogEvent) ? ((MutableLogEvent) event).createMemento() : event);
}
项目:jabref    文件:LogMessages.java   
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);
}
项目:logging-log4j2    文件:AsyncLoggerConfigDisruptor.java   
public Log4jEventWrapper(final MutableLogEvent mutableLogEvent) {
    event = mutableLogEvent;
}
项目:logging-log4j2    文件:AsyncLoggerConfigDisruptor.java   
@Override
public Log4jEventWrapper newInstance() {
    return new Log4jEventWrapper(new MutableLogEvent());
}
项目:logging-log4j2    文件:AsyncLoggerConfigDisruptor.java   
@Override
public void translateTo(final Log4jEventWrapper ringBufferElement, final long sequence,
        final LogEvent logEvent, final AsyncLoggerConfig loggerConfig) {
    ((MutableLogEvent) ringBufferElement.event).initFrom(logEvent);
    ringBufferElement.loggerConfig = loggerConfig;
}