Skip to content

Commit

Permalink
[PAXLOGGING-239] Use WeakReferences in org.apache.logging.log4j.core.…
Browse files Browse the repository at this point in the history
…impl.ReusableLogEventFactory
  • Loading branch information
grgrzybek committed Jun 8, 2017
1 parent d9456e4 commit 50c16e0
Showing 1 changed file with 6 additions and 3 deletions.
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.logging.log4j.core.impl;

import java.lang.ref.WeakReference;
import java.util.List;

import org.apache.logging.log4j.Level;
Expand All @@ -39,7 +40,7 @@ public class ReusableLogEventFactory implements LogEventFactory {
private static final ThreadNameCachingStrategy THREAD_NAME_CACHING_STRATEGY = ThreadNameCachingStrategy.create();
private static final Clock CLOCK = ClockFactory.getClock();

private static ThreadLocal<MutableLogEvent> mutableLogEventThreadLocal = new ThreadLocal<>();
private static ThreadLocal<WeakReference<MutableLogEvent>> mutableLogEventThreadLocal = new ThreadLocal<>();
private final ContextDataInjector injector = ContextDataInjectorFactory.createInjector();

/**
Expand All @@ -58,7 +59,8 @@ public class ReusableLogEventFactory implements LogEventFactory {
public LogEvent createEvent(final String loggerName, final Marker marker,
final String fqcn, final Level level, final Message message,
final List<Property> properties, final Throwable t) {
MutableLogEvent result = mutableLogEventThreadLocal.get();
WeakReference<MutableLogEvent> refResult = mutableLogEventThreadLocal.get();
MutableLogEvent result = refResult == null ? null : refResult.get();
if (result == null || result.reserved) {
final boolean initThreadLocal = result == null;
result = new MutableLogEvent();
Expand All @@ -68,7 +70,8 @@ public LogEvent createEvent(final String loggerName, final Marker marker,
result.setThreadName(Thread.currentThread().getName()); // Thread.getName() allocates Objects on each call
result.setThreadPriority(Thread.currentThread().getPriority());
if (initThreadLocal) {
mutableLogEventThreadLocal.set(result);
refResult = new WeakReference<>(result);
mutableLogEventThreadLocal.set(refResult);
}
}
result.reserved = true;
Expand Down

0 comments on commit 50c16e0

Please sign in to comment.