logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1452155 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java src/changes/changes.xml
Date Mon, 04 Mar 2013 00:00:54 GMT
Author: rgoers
Date: Mon Mar  4 00:00:54 2013
New Revision: 1452155

URL: http://svn.apache.org/r1452155
Log:
LOG4J2-167 - Configurator throws a ClassCastException if LogManager returns a SimpleLoggerContext.

Added:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java?rev=1452155&r1=1452154&r2=1452155&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
Mon Mar  4 00:00:54 2013
@@ -17,15 +17,22 @@
 package org.apache.logging.log4j.core.config;
 
 import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.LoggingException;
 import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.status.StatusData;
+import org.apache.logging.log4j.status.StatusLogger;
+
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.List;
 
 /**
  * Initializes and configure the Logging system.
  */
 public final class Configurator {
 
+    private static final StatusLogger LOGGER = StatusLogger.getLogger();
+
     private Configurator() {
     }
 
@@ -57,10 +64,16 @@ public final class Configurator {
     public static LoggerContext initialize(final String name, final ClassLoader loader, final
URI configLocation) {
 
         try {
-            final LoggerContext ctx = (LoggerContext) LogManager.getContext(loader, false,
configLocation);
-            final Configuration config = ConfigurationFactory.getInstance().getConfiguration(name,
configLocation);
-            ctx.setConfiguration(config);
-            return ctx;
+            org.apache.logging.log4j.spi.LoggerContext context = LogManager.getContext(loader,
false, configLocation);
+            if (context instanceof LoggerContext) {
+                final LoggerContext ctx = (LoggerContext) context;
+                final Configuration config = ConfigurationFactory.getInstance().getConfiguration(name,
configLocation);
+                ctx.setConfiguration(config);
+                return ctx;
+            } else {
+                LOGGER.error("LogManager returned an instance of {} which does not implement
{}. Unable to initialize Log4j",
+                    context.getClass().getName(), LoggerContext.class.getName());
+            }
         } catch (final Exception ex) {
             ex.printStackTrace();
         }
@@ -83,10 +96,16 @@ public final class Configurator {
             } catch (Exception ex) {
                 // Invalid source location.
             }
-            final LoggerContext ctx = (LoggerContext) LogManager.getContext(loader, false,
configLocation);
-            final Configuration config = ConfigurationFactory.getInstance().getConfiguration(source);
-            ctx.setConfiguration(config);
-            return ctx;
+            org.apache.logging.log4j.spi.LoggerContext context = LogManager.getContext(loader,
false, configLocation);
+            if (context instanceof LoggerContext) {
+                final LoggerContext ctx = (LoggerContext) context;
+                final Configuration config = ConfigurationFactory.getInstance().getConfiguration(source);
+                ctx.setConfiguration(config);
+                return ctx;
+            } else {
+                LOGGER.error("LogManager returned an instance of {} which does not implement
{}. Unable to initialize Log4j",
+                    context.getClass().getName(), LoggerContext.class.getName());
+            }
         } catch (final Exception ex) {
             ex.printStackTrace();
         }

Added: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java?rev=1452155&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java
Mon Mar  4 00:00:54 2013
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.config;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.simple.SimpleLoggerContextFactory;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.Map;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class TestConfiguratorError {
+
+    private static final String FACTORY_PROPERTY_NAME =  "log4j2.loggerContextFactory";
+
+    @BeforeClass
+    public static void beforeClass() {
+        System.setProperty(FACTORY_PROPERTY_NAME, SimpleLoggerContextFactory.class.getName());
+    }
+
+    @Test
+    public void testError() throws Exception {
+        final LoggerContext ctx = Configurator.initialize("Test1", null, "target/test-classes/log4j2-config.xml");
+        assertNull("No LoggerContext should have been returned", ctx);
+    }
+}

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1452155&r1=1452154&r2=1452155&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Mon Mar  4 00:00:54 2013
@@ -23,6 +23,9 @@
 
   <body>
     <release version="2.0-beta5" date="@TBD@" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-167" dev="rgoers" type="fix">
+        Configurator throws a ClassCastException if LogManager returns a SimpleLoggerContext.
+      </action>
       <action issue="LOG4J2-169" dev="rgoers" type="fix">
         ConfigurationFactory was adding factories on every call.
       </action>



Mime
View raw message