LOG4J2-1171 use servlet context name for logger context name when available
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/6cfedcf7 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/6cfedcf7 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/6cfedcf7 Branch: refs/heads/master Commit: 6cfedcf7a5d28f9e4212829c2ad1b2e47bb64c41 Parents: 129ae4a Author: rpopma <rpo...@apache.org> Authored: Fri Oct 23 23:50:11 2015 +0900 Committer: rpopma <rpo...@apache.org> Committed: Fri Oct 23 23:50:11 2015 +0900 ---------------------------------------------------------------------- .../logging/log4j/core/LoggerContext.java | 12 +++++- .../log4j/core/async/AsyncLoggerContext.java | 6 +++ .../log4j/core/async/AsyncLoggerHelper.java | 41 ++++++++++++-------- .../log4j/core/impl/Log4jContextFactory.java | 3 ++ src/changes/changes.xml | 3 ++ 5 files changed, 48 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6cfedcf7/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java index 872c01d..6605129 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java @@ -74,7 +74,7 @@ public class LoggerContext extends AbstractLifeCycle implements org.apache.loggi */ private volatile Configuration configuration = new DefaultConfiguration(); private Object externalContext; - private final String contextName; + private String contextName; private volatile URI configLocation; private Cancellable shutdownCallback; @@ -318,6 +318,16 @@ public class LoggerContext extends AbstractLifeCycle implements org.apache.loggi public String getName() { return contextName; } + + /** + * Sets the name. + * + * @param name the new LoggerContext name + * @throws NullPointerException if the specified name is {@code null} + */ + public void setName(final String name) { + contextName = Objects.requireNonNull(name); + } /** * Sets the external context. http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6cfedcf7/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerContext.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerContext.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerContext.java index 4efd7cc..d5bddb5 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerContext.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerContext.java @@ -57,6 +57,12 @@ public class AsyncLoggerContext extends LoggerContext { protected Logger newInstance(final LoggerContext ctx, final String name, final MessageFactory messageFactory) { return new AsyncLogger(ctx, name, messageFactory, helper); } + + @Override + public void setName(final String name) { + super.setName("AsyncContext[" + name + "]"); + helper.setContextName(name); + } /* * (non-Javadoc) http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6cfedcf7/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerHelper.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerHelper.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerHelper.java index 027fa0d..5c90eae 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerHelper.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerHelper.java @@ -41,14 +41,14 @@ import com.lmax.disruptor.dsl.ProducerType; * life cycle of the context. The AsyncLoggerHelper of the context is shared by all AsyncLogger objects created by that * AsyncLoggerContext. */ -public class AsyncLoggerHelper { +class AsyncLoggerHelper { private static final int SLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTS = 50; private static final int MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN = 200; private static final int RINGBUFFER_MIN_SIZE = 128; private static final int RINGBUFFER_DEFAULT_SIZE = 256 * 1024; private static final StatusLogger LOGGER = StatusLogger.getLogger(); - private final String contextName; + private String contextName; private ExecutorService executor; private volatile Disruptor<RingBufferLogEvent> disruptor; @@ -60,6 +60,10 @@ public class AsyncLoggerHelper { return contextName; } + public void setContextName(String name) { + contextName = name; + } + Disruptor<RingBufferLogEvent> getDisruptor() { return disruptor; } @@ -77,18 +81,22 @@ public class AsyncLoggerHelper { LOGGER.trace("[{}] AsyncLoggerHelper creating new disruptor.", contextName); final int ringBufferSize = calculateRingBufferSize(); final WaitStrategy waitStrategy = createWaitStrategy(); - executor = Executors.newSingleThreadExecutor(new DaemonThreadFactory(contextName + "-Logger-")); + executor = Executors.newSingleThreadExecutor(new DaemonThreadFactory("AsyncLogger[" + contextName + "]")); Info.initExecutorThreadInstance(executor); disruptor = new Disruptor<>(RingBufferLogEvent.FACTORY, ringBufferSize, executor, ProducerType.MULTI, waitStrategy); - disruptor.handleExceptionsWith(getExceptionHandler()); + + final ExceptionHandler<RingBufferLogEvent> errorHandler = getExceptionHandler(); + disruptor.handleExceptionsWith(errorHandler); final RingBufferLogEventHandler[] handlers = {new RingBufferLogEventHandler()}; disruptor.handleEventsWith(handlers); - LOGGER.debug("[{}] Starting AsyncLogger disruptor with ringbuffer size {}...", contextName, disruptor - .getRingBuffer().getBufferSize()); + LOGGER.debug( + "[{}] Starting AsyncLogger disruptor with ringbufferSize={}, waitStrategy={}, exceptionHandler={}...", + contextName, disruptor.getRingBuffer().getBufferSize(), waitStrategy.getClass().getSimpleName(), + errorHandler); disruptor.start(); } @@ -113,22 +121,22 @@ public class AsyncLoggerHelper { private WaitStrategy createWaitStrategy() { final String strategy = PropertiesUtil.getProperties().getStringProperty("AsyncLogger.WaitStrategy"); - LOGGER.debug("[{}] property AsyncLogger.WaitStrategy={}", contextName, strategy); - if ("Sleep".equals(strategy)) { - return new SleepingWaitStrategy(); - } else if ("Yield".equals(strategy)) { - return new YieldingWaitStrategy(); - } else if ("Block".equals(strategy)) { - return new BlockingWaitStrategy(); + if (strategy != null) { + LOGGER.trace("[{}] property AsyncLogger.WaitStrategy={}", contextName, strategy); + if ("Sleep".equals(strategy)) { + return new SleepingWaitStrategy(); + } else if ("Yield".equals(strategy)) { + return new YieldingWaitStrategy(); + } else if ("Block".equals(strategy)) { + return new BlockingWaitStrategy(); + } } - LOGGER.debug("[{}] disruptor event handler uses BlockingWaitStrategy", contextName); return new BlockingWaitStrategy(); } private ExceptionHandler<RingBufferLogEvent> getExceptionHandler() { final String cls = PropertiesUtil.getProperties().getStringProperty("AsyncLogger.ExceptionHandler"); if (cls == null) { - LOGGER.debug("[{}] No AsyncLogger.ExceptionHandler specified", contextName); return null; } try { @@ -163,7 +171,8 @@ public class AsyncLoggerHelper { * @return a new {@code RingBufferAdmin} that instruments the ringbuffer */ public RingBufferAdmin createRingBufferAdmin(final String contextName) { - return RingBufferAdmin.forAsyncLogger(disruptor.getRingBuffer(), contextName); + final RingBuffer<RingBufferLogEvent> ring = disruptor == null ? null : disruptor.getRingBuffer(); + return RingBufferAdmin.forAsyncLogger(ring, contextName); } /** http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6cfedcf7/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java index 5c6ebfe..f54fa2d 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java @@ -222,6 +222,9 @@ public class Log4jContextFactory implements LoggerContextFactory, ShutdownCallba if (externalContext != null && ctx.getExternalContext() == null) { ctx.setExternalContext(externalContext); } + if (name != null) { + ctx.setName(name); + } if (ctx.getState() == LifeCycle.State.INITIALIZED) { if (configLocation != null || name != null) { ContextAnchor.THREAD_CONTEXT.set(ctx); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6cfedcf7/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 3f37ffb..20725df 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -24,6 +24,9 @@ </properties> <body> <release version="2.5" date="2015-MM-DD" description="GA Release 2.5"> + <action issue="LOG4J2-1171" dev="rpopma" type="fix"> + Use servlet context name for logger context name when available. + </action> <action issue="LOG4J2-1159" dev="rpopma" type="fix"> Fixed a ThreadLocal memory leak in Tomcat8 that mentions AsyncLoggers when Async Loggers are not used. </action>