org.apache.logging.log4j.message.ReusableMessage Java Examples

The following examples show how to use org.apache.logging.log4j.message.ReusableMessage. 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: MemoryAppender.java    From syncope with Apache License 2.0 6 votes vote down vote up
@Override
public void append(final LogEvent event) {
    LogStatement statement = new LogStatement();

    statement.setLevel(LoggerLevel.fromLevel(event.getLevel()));
    statement.setLoggerName(event.getLoggerName());

    Message msg = event.getMessage();
    statement.setMessage((msg instanceof ReusableMessage
            ? ((ReusableMessage) msg).memento()
            : msg).getFormattedMessage());

    statement.setTimeMillis(event.getTimeMillis());

    if (event.getThrown() != null) {
        statement.setStackTrace(ExceptionUtils2.getFullStackTrace(event.getThrown()));
    }

    statement.setThreadId(event.getThreadId());
    statement.setThreadName(event.getThreadName());
    statement.setThreadPriority(event.getThreadPriority());

    this.statements.add(statement);
}
 
Example #2
Source File: Log4jLogEvent.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
public LogEventProxy(final Log4jLogEvent event, final boolean includeLocation) {
    this.loggerFQCN = event.loggerFqcn;
    this.marker = event.marker;
    this.level = event.level;
    this.loggerName = event.loggerName;
    this.message = event.message instanceof ReusableMessage
            ? memento((ReusableMessage) event.message)
            : event.message;
    this.timeMillis = event.instant.getEpochMillisecond();
    this.nanoOfMillisecond = event.instant.getNanoOfMillisecond();
    this.thrown = event.thrown;
    this.thrownProxy = event.thrownProxy;
    this.contextData = event.contextData;
    this.contextStack = event.contextStack;
    this.source = includeLocation ? event.getSource() : null;
    this.threadId = event.getThreadId();
    this.threadName = event.getThreadName();
    this.threadPriority = event.getThreadPriority();
    this.isLocationRequired = includeLocation;
    this.isEndOfBatch = event.endOfBatch;
    this.nanoTime = event.nanoTime;
}
 
Example #3
Source File: Log4jLogEvent.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
public LogEventProxy(final LogEvent event, final boolean includeLocation) {
    this.loggerFQCN = event.getLoggerFqcn();
    this.marker = event.getMarker();
    this.level = event.getLevel();
    this.loggerName = event.getLoggerName();

    final Message temp = event.getMessage();
    message = temp instanceof ReusableMessage
            ? memento((ReusableMessage) temp)
            : temp;
    this.timeMillis = event.getInstant().getEpochMillisecond();
    this.nanoOfMillisecond = event.getInstant().getNanoOfMillisecond();
    this.thrown = event.getThrown();
    this.thrownProxy = event.getThrownProxy();
    this.contextData = memento(event.getContextData());
    this.contextStack = event.getContextStack();
    this.source = includeLocation ? event.getSource() : null;
    this.threadId = event.getThreadId();
    this.threadName = event.getThreadName();
    this.threadPriority = event.getThreadPriority();
    this.isLocationRequired = includeLocation;
    this.isEndOfBatch = event.isEndOfBatch();
    this.nanoTime = event.getNanoTime();
}
 
Example #4
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 #5
Source File: ReusableParameterizedMessageMemoryLeakTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("UnusedAssignment") // parameter set to null to allow garbage collection
public void testParametersAreNotLeaked() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    ReusableMessage message = (ReusableMessage) ReusableMessageFactory.INSTANCE.newMessage(
            "foo {}", new ParameterObject("paramValue", latch));
    // Large enough for the parameters, but smaller than the default reusable array size.
    message.swapParameters(new Object[5]);
    GarbageCollectionHelper gcHelper = new GarbageCollectionHelper();
    gcHelper.run();
    try {
        assertTrue("Parameter should have been garbage collected", latch.await(30, TimeUnit.SECONDS));
    } finally {
        gcHelper.close();
    }
}
 
Example #6
Source File: LambdaLoggerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public LogEvent(final String fqcn, final Level level, final Marker marker, final Message message, final Throwable t) {
    this.fqcn = fqcn;
    this.level = level;
    this.marker = marker;
    this.message = (message instanceof ReusableMessage) ?
            ((ReusableMessage) message).memento() :
            message;
    this.throwable = t;
}
 
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: Log4jLogEvent.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public Log4jLogEvent toImmutable() {
    if (getMessage() instanceof ReusableMessage) {
        makeMessageImmutable();
    }
    return this;
}
 
Example #9
Source File: Log4jLogEventTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testToImmutableNotSame() {
    final LogEvent logEvent = new Log4jLogEvent.Builder().setMessage(new ReusableObjectMessage()).build();
    final LogEvent immutable = logEvent.toImmutable();
    Assert.assertSame(logEvent, immutable);
    Assert.assertFalse(immutable.getMessage() instanceof ReusableMessage);
}
 
Example #10
Source File: Log4jLogEvent.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private static Message memento(final ReusableMessage message) {
    return message.memento();
}
 
Example #11
Source File: AsyncLogger.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private boolean isReused(final Message message) {
    return message instanceof ReusableMessage;
}