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/09/08 00:39:03 UTC

[24/32] git commit: Move JUL LogManager init to constructor.

Move JUL LogManager init to constructor.

  - Allow overriding of the LoggerAdapter used.
  - Store adapter as instance field instead of static field.
  - Fix class names that were changed recently.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/81b1d8b9
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/81b1d8b9
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/81b1d8b9

Branch: refs/heads/master
Commit: 81b1d8b95948f96920cc0df77775f8f6789c016a
Parents: fdafda8
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 7 14:42:58 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 7 14:42:58 2014 -0500

----------------------------------------------------------------------
 .../apache/logging/log4j/jdk/LogManager.java    | 66 +++++++++++++-------
 1 file changed, 45 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/81b1d8b9/log4j-jul/src/main/java/org/apache/logging/log4j/jdk/LogManager.java
----------------------------------------------------------------------
diff --git a/log4j-jul/src/main/java/org/apache/logging/log4j/jdk/LogManager.java b/log4j-jul/src/main/java/org/apache/logging/log4j/jdk/LogManager.java
index 8d2ae43..2657a1c 100644
--- a/log4j-jul/src/main/java/org/apache/logging/log4j/jdk/LogManager.java
+++ b/log4j-jul/src/main/java/org/apache/logging/log4j/jdk/LogManager.java
@@ -20,9 +20,10 @@ import java.util.Collections;
 import java.util.Enumeration;
 import java.util.logging.Logger;
 
-import org.apache.logging.log4j.spi.LoggerAdapter;
+import org.apache.logging.log4j.LoggingException;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.apache.logging.log4j.util.LoaderUtil;
+import org.apache.logging.log4j.util.PropertiesUtil;
 
 /**
  * Log4j implementation of {@link java.util.logging.LogManager}. Note that the system property
@@ -30,32 +31,55 @@ import org.apache.logging.log4j.util.LoaderUtil;
  * this adaptor. This LogManager requires the {@code log4j-api} library to be available. If {@code log4j-core} is
  * also available, then more features of {@link java.util.logging.Logger} are supported.
  *
+ * <p>To override the default {@link AbstractLoggerAdapter} that is used, specify the Log4j property
+ * {@code org.apache.logging.log4j.jdk.LoggerAdapter} and set it to the fully qualified class name of a custom
+ * implementation. All implementations must have a default constructor.</p>
+ *
  * @since 2.1
  */
 public class LogManager extends java.util.logging.LogManager {
 
-    private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
-    private static final LoggerAdapter<Logger> ADAPTER;
+    /**
+     * Name of the Log4j property to set to override the {@link AbstractLoggerAdapter} to be used. By
+     * default, when this property is not set, an appropriate LoggerAdaptor is chosen based on the presence of
+     * {@code log4j-core}.
+     */
+    public static final String LOGGER_ADAPTOR_PROPERTY = "org.apache.logging.log4j.jdk.LoggerAdapter";
 
-    static {
-        // find out if log4j-core is available
-        String registryClassName;
-        try {
-            LoaderUtil.loadClass("org.apache.logging.log4j.core.Logger");
-            registryClassName = "org.apache.logging.log4j.jdk.CoreLoggerRegistry";
-        } catch (final ClassNotFoundException ignored) {
-            registryClassName = "org.apache.logging.log4j.jdk.ApiLoggerRegistry";
-        }
-        LOGGER.debug("Attempting to use {}", registryClassName);
-        try {
-            ADAPTER = LoaderUtil.newCheckedInstanceOf(registryClassName, AbstractLoggerAdapter.class);
-        } catch (final Exception e) {
-            throw LOGGER.throwing(new ExceptionInInitializerError(e));
-        }
-    }
+    private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
+    private final AbstractLoggerAdapter loggerAdapter;
 
     public LogManager() {
         super();
+        AbstractLoggerAdapter adapter = null;
+        final String overrideAdaptorClassName =
+            PropertiesUtil.getProperties().getStringProperty(LOGGER_ADAPTOR_PROPERTY);
+        if (overrideAdaptorClassName != null) {
+            try {
+                LOGGER.info("Trying to use LoggerAdaptor [{}] specified by Log4j property.", overrideAdaptorClassName);
+                adapter = LoaderUtil.newCheckedInstanceOf(overrideAdaptorClassName, AbstractLoggerAdapter.class);
+            } catch (final Exception e) {
+                LOGGER.error("Specified LoggerAdapter [{}] is incompatible.", overrideAdaptorClassName, e);
+            }
+        }
+        if (adapter == null) {
+            // default adapter
+            String adapterClassName;
+            try {
+                // find out if log4j-core is available
+                LoaderUtil.loadClass("org.apache.logging.log4j.core.Logger");
+                adapterClassName = "org.apache.logging.log4j.jdk.CoreLoggerAdapter";
+            } catch (final ClassNotFoundException ignored) {
+                adapterClassName = "org.apache.logging.log4j.jdk.ApiLoggerAdapter";
+            }
+            LOGGER.debug("Attempting to use {}", adapterClassName);
+            try {
+                adapter = LoaderUtil.newCheckedInstanceOf(adapterClassName, AbstractLoggerAdapter.class);
+            } catch (final Exception e) {
+                throw LOGGER.throwing(new LoggingException(e));
+            }
+        }
+        loggerAdapter = adapter;
         LOGGER.info("Registered Log4j as the java.util.logging.LogManager.");
     }
 
@@ -69,12 +93,12 @@ public class LogManager extends java.util.logging.LogManager {
     @Override
     public Logger getLogger(final String name) {
         LOGGER.trace("Call to LogManager.getLogger({})", name);
-        return ADAPTER.getLogger(name);
+        return loggerAdapter.getLogger(name);
     }
 
     @Override
     public Enumeration<String> getLoggerNames() {
-        return Collections.enumeration(ADAPTER.getLoggersInContext(ADAPTER.getContext()).keySet());
+        return Collections.enumeration(loggerAdapter.getLoggersInContext(loggerAdapter.getContext()).keySet());
     }
 
 }