You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2014/08/22 04:35:52 UTC

svn commit: r1619653 - /logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java

Author: mattsicker
Date: Fri Aug 22 02:35:51 2014
New Revision: 1619653

URL: http://svn.apache.org/r1619653
Log:
Update LoggerContext to use ShutdownRegistrationStrategy.

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java?rev=1619653&r1=1619652&r2=1619653&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java Fri Aug 22 02:35:51 2014
@@ -30,8 +30,6 @@ import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
 import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Marker;
-import org.apache.logging.log4j.MarkerManager;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationFactory;
 import org.apache.logging.log4j.core.config.ConfigurationListener;
@@ -41,11 +39,16 @@ import org.apache.logging.log4j.core.con
 import org.apache.logging.log4j.core.config.Reconfigurable;
 import org.apache.logging.log4j.core.jmx.Server;
 import org.apache.logging.log4j.core.util.Assert;
+import org.apache.logging.log4j.core.util.DefaultShutdownRegistrationStrategy;
+import org.apache.logging.log4j.core.util.Loader;
 import org.apache.logging.log4j.core.util.NetUtils;
+import org.apache.logging.log4j.core.util.ShutdownRegistrationStrategy;
 import org.apache.logging.log4j.message.MessageFactory;
 import org.apache.logging.log4j.spi.AbstractLogger;
 import org.apache.logging.log4j.util.PropertiesUtil;
 
+import static org.apache.logging.log4j.core.util.ShutdownRegistrationStrategy.SHUTDOWN_HOOK_MARKER;
+
 /**
  * The LoggerContext is the anchor for the logging system. It maintains a list
  * of all the loggers requested by applications and a reference to the
@@ -56,10 +59,9 @@ import org.apache.logging.log4j.util.Pro
 public class LoggerContext extends AbstractLifeCycle implements org.apache.logging.log4j.spi.LoggerContext, ConfigurationListener {
 
     private static final boolean SHUTDOWN_HOOK_ENABLED =
-        PropertiesUtil.getProperties().getBooleanProperty("log4j.shutdownHookEnabled", true);
+        PropertiesUtil.getProperties().getBooleanProperty(ShutdownRegistrationStrategy.SHUTDOWN_HOOK_ENABLED, true);
 
     public static final String PROPERTY_CONFIG = "config";
-    private static final Marker SHUTDOWN_HOOK = MarkerManager.getMarker("SHUTDOWN HOOK");
     private static final Configuration NULL_CONFIGURATION = new NullConfiguration();
 
     private final ConcurrentMap<String, Logger> loggers = new ConcurrentHashMap<String, Logger>();
@@ -80,6 +82,7 @@ public class LoggerContext extends Abstr
      * SoftReference here to prevent possible cyclic memory references.
      */
     private Reference<Thread> shutdownThread;
+    private ShutdownRegistrationStrategy shutdownRegistrationStrategy;
 
     private final Lock configLock = new ReentrantLock();
 
@@ -176,7 +179,7 @@ public class LoggerContext extends Abstr
 
     private void setUpShutdownHook() {
         if (config.isShutdownHookEnabled() && SHUTDOWN_HOOK_ENABLED) {
-            LOGGER.debug(SHUTDOWN_HOOK, "Shutdown hook enabled. Registering a new one.");
+            LOGGER.debug(SHUTDOWN_HOOK_MARKER, "Shutdown hook enabled. Registering a new one.");
             shutdownThread = new SoftReference<Thread>(
                     new Thread(new ShutdownThread(this), "log4j-shutdown")
             );
@@ -187,12 +190,24 @@ public class LoggerContext extends Abstr
     private void addShutdownHook() {
         final Thread hook = getShutdownThread();
         if (hook != null) {
+            final String shutdownStrategyClassName = PropertiesUtil.getProperties().getStringProperty(
+                ShutdownRegistrationStrategy.SHUTDOWN_REGISTRATION_STRATEGY);
+            if (shutdownStrategyClassName != null) {
+                try {
+                    shutdownRegistrationStrategy =
+                        Loader.newCheckedInstanceOf(shutdownStrategyClassName, ShutdownRegistrationStrategy.class);
+                } catch (final Exception e) {
+                    LOGGER.error(SHUTDOWN_HOOK_MARKER, "There was an error loading the ShutdownRegistrationStrategy [{}]. " +
+                        "Falling back to DefaultShutdownRegistrationStrategy.", shutdownStrategyClassName, e);
+                    shutdownRegistrationStrategy = new DefaultShutdownRegistrationStrategy();
+                }
+            }
             try {
-                Runtime.getRuntime().addShutdownHook(hook);
+                shutdownRegistrationStrategy.registerShutdownHook(hook);
             } catch (final IllegalStateException ise) {
-                LOGGER.warn(SHUTDOWN_HOOK, "Unable to register shutdown hook due to JVM state");
+                LOGGER.fatal(SHUTDOWN_HOOK_MARKER, "Unable to register shutdown hook because JVM is shutting down.");
             } catch (final SecurityException se) {
-                LOGGER.warn(SHUTDOWN_HOOK, "Unable to register shutdown hook due to security restrictions");
+                LOGGER.error(SHUTDOWN_HOOK_MARKER, "Unable to register shutdown hook due to security restrictions");
             }
         }
     }
@@ -228,8 +243,9 @@ public class LoggerContext extends Abstr
     }
 
     private void tearDownShutdownHook() {
-        if (shutdownThread != null) {
-            LOGGER.debug(SHUTDOWN_HOOK, "Enqueue shutdown hook for garbage collection.");
+        if (shutdownThread != null && shutdownThread.get() != null) {
+            shutdownRegistrationStrategy.unregisterShutdownHook(shutdownThread.get());
+            LOGGER.debug(SHUTDOWN_HOOK_MARKER, "Enqueue shutdown hook for garbage collection.");
             shutdownThread.enqueue();
         }
     }