Author: rgoers
Date: Wed Aug  8 04:28:22 2012
New Revision: 1370663

URL: http://svn.apache.org/viewvc?rev=1370663&view=rev
Log:
Fix Log4j2-65

Added:
    
logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationPropsTest.java
      - copied, changed from r1370375, 
logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java
    logging/log4j/log4j2/trunk/core/src/test/resources/log4j-props.xml
Modified:
    
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
    
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
    
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
    
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
    
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java?rev=1370663&r1=1370662&r2=1370663&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
 (original)
+++ 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
 Wed Aug  8 04:28:22 2012
@@ -83,7 +83,9 @@ public class BaseConfiguration extends F
 
     private ConcurrentMap<String, LoggerConfig> loggers = new 
ConcurrentHashMap<String, LoggerConfig>();
 
-    private StrSubstitutor subst = new StrSubstitutor();
+    private StrLookup tempLookup = new Interpolator();
+
+    private StrSubstitutor subst = new StrSubstitutor(tempLookup);
 
     private LoggerConfig root = new LoggerConfig();
 
@@ -150,13 +152,13 @@ public class BaseConfiguration extends F
                 continue;
             }
             if (child.getName().equalsIgnoreCase("properties")) {
-                if (subst.getVariableResolver() == null) {
+                if (tempLookup == subst.getVariableResolver()) {
                     subst.setVariableResolver((StrLookup) child.getObject());
                 } else {
                     LOGGER.error("Properties declaration must be the first 
element in the configuration");
                 }
                 continue;
-            } else if (subst.getVariableResolver() == null) {
+            } else if (tempLookup == subst.getVariableResolver()) {
                 subst.setVariableResolver(new Interpolator(null));
             }
             if (child.getName().equalsIgnoreCase("appenders")) {

Modified: 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java?rev=1370663&r1=1370662&r2=1370663&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
 (original)
+++ 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
 Wed Aug  8 04:28:22 2012
@@ -51,6 +51,8 @@ public class JSONConfiguration extends B
 
     private JsonNode root;
 
+    private List<String> messages = new ArrayList<String>();
+
     public JSONConfiguration(InputSource source, File configFile) {
         byte[] buffer;
 
@@ -69,18 +71,22 @@ public class JSONConfiguration extends B
             boolean verbose = false;
             for (Map.Entry<String, String> entry : 
rootNode.getAttributes().entrySet()) {
                 if ("status".equalsIgnoreCase(entry.getKey())) {
-                    status = Level.toLevel(entry.getValue().toUpperCase(), 
Level.OFF);
+                    status = 
Level.toLevel(getSubst().replace(entry.getValue()).toUpperCase(), null);
+                    if (status == null) {
+                        status = Level.ERROR;
+                        messages.add("Invalid status specified: " + 
entry.getValue() + ". Defaulting to ERROR");
+                    }
                 } else if ("verbose".equalsIgnoreCase(entry.getKey())) {
-                    verbose = Boolean.parseBoolean(entry.getValue());
+                    verbose = 
Boolean.parseBoolean(getSubst().replace(entry.getValue()));
                 } else if ("packages".equalsIgnoreCase(entry.getKey())) {
-                    String[] packages = entry.getValue().split(",");
+                    String[] packages = 
getSubst().replace(entry.getValue()).split(",");
                     for (String p : packages) {
                         PluginManager.addPackage(p);
                     }
                 } else if ("name".equalsIgnoreCase(entry.getKey())) {
-                    setName(entry.getValue());
+                    setName(getSubst().replace(entry.getValue()));
                 } else if ("monitorInterval".equalsIgnoreCase(entry.getKey())) 
{
-                    int interval = Integer.parseInt(entry.getValue());
+                    int interval = 
Integer.parseInt(getSubst().replace(entry.getValue()));
                     if (interval > 0 && configFile != null) {
                         monitor = new FileConfigurationMonitor(configFile, 
listeners, interval);
                     }
@@ -105,6 +111,9 @@ public class JSONConfiguration extends B
                     listener.setFilters(VERBOSE_CLASSES);
                 }
                 ((StatusLogger) LOGGER).registerListener(listener);
+                for (String msg : messages) {
+                    LOGGER.error(msg);
+                }
             }
             if (getName() == null) {
                 setName(source.getSystemId());

Modified: 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java?rev=1370663&r1=1370662&r2=1370663&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
 (original)
+++ 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
 Wed Aug  8 04:28:22 2012
@@ -360,7 +360,13 @@ public class LoggerConfig extends Filter
         }
 
         List<AppenderRef> appenderRefs = Arrays.asList(refs);
-        Level level = loggerLevel == null ? Level.ERROR : 
Level.valueOf(loggerLevel.toUpperCase());
+        Level level;
+        try {
+            level = loggerLevel == null ? Level.ERROR : 
Level.valueOf(loggerLevel.toUpperCase());
+        } catch (Exception ex) {
+            LOGGER.error("Invalid Log level specified: {}. Defaulting to 
Error", loggerLevel);
+            level = Level.ERROR;
+        }
         String name = loggerName.equals("root") ? "" : loggerName;
         boolean additive = additivity == null ? true : 
Boolean.parseBoolean(additivity);
 
@@ -379,7 +385,13 @@ public class LoggerConfig extends Filter
                                             @PluginElement("appender-ref") 
AppenderRef[] refs,
                                             @PluginElement("filters") Filter 
filter) {
             List<AppenderRef> appenderRefs = Arrays.asList(refs);
-            Level level = loggerLevel == null ? Level.ERROR : 
Level.valueOf(loggerLevel.toUpperCase());
+            Level level;
+            try {
+                level = loggerLevel == null ? Level.ERROR : 
Level.valueOf(loggerLevel.toUpperCase());
+            } catch (Exception ex) {
+                LOGGER.error("Invalid Log level specified: {}. Defaulting to 
Error", loggerLevel);
+                level = Level.ERROR;
+            }
             boolean additive = additivity == null ? true : 
Boolean.parseBoolean(additivity);
 
             return new LoggerConfig(LogManager.ROOT_LOGGER_NAME, appenderRefs, 
filter, level, additive);

Modified: 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java?rev=1370663&r1=1370662&r2=1370663&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
 (original)
+++ 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
 Wed Aug  8 04:28:22 2012
@@ -72,6 +72,8 @@ public class XMLConfiguration extends Ba
 
     private Validator validator;
 
+    private List<String> messages = new ArrayList<String>();
+
     public XMLConfiguration(InputSource source, File configFile) {
         byte[] buffer = null;
 
@@ -87,22 +89,26 @@ public class XMLConfiguration extends Ba
 
             for (Map.Entry<String, String> entry : attrs.entrySet()) {
                 if ("status".equalsIgnoreCase(entry.getKey())) {
-                    status = Level.toLevel(entry.getValue().toUpperCase(), 
Level.OFF);
+                    status = 
Level.toLevel(getSubst().replace(entry.getValue()).toUpperCase(), null);
+                    if (status == null) {
+                        status = Level.ERROR;
+                        messages.add("Invalid status specified: " + 
entry.getValue() + ". Defaulting to ERROR");
+                    }
                 } else if ("verbose".equalsIgnoreCase(entry.getKey())) {
-                    verbose = Boolean.parseBoolean(entry.getValue());
-                } else if ("packages".equalsIgnoreCase(entry.getKey())) {
+                    verbose = 
Boolean.parseBoolean(getSubst().replace(entry.getValue()));
+                } else if 
("packages".equalsIgnoreCase(getSubst().replace(entry.getKey()))) {
                     String[] packages = entry.getValue().split(",");
                     for (String p : packages) {
                         PluginManager.addPackage(p);
                     }
                 } else if ("name".equalsIgnoreCase(entry.getKey())) {
-                    setName(entry.getValue());
+                    setName(getSubst().replace(entry.getValue()));
                 } else if ("strict".equalsIgnoreCase(entry.getKey())) {
-                    strict = Boolean.parseBoolean(entry.getValue());
+                    strict = 
Boolean.parseBoolean(getSubst().replace(entry.getValue()));
                 } else if ("schema".equalsIgnoreCase(entry.getKey())) {
-                    schema = entry.getValue();
+                    schema = getSubst().replace(entry.getValue());
                 } else if ("monitorInterval".equalsIgnoreCase(entry.getKey())) 
{
-                    int interval = Integer.parseInt(entry.getValue());
+                    int interval = 
Integer.parseInt(getSubst().replace(entry.getValue()));
                     if (interval > 0 && configFile != null) {
                         monitor = new FileConfigurationMonitor(configFile, 
listeners, interval);
                     }
@@ -126,6 +132,9 @@ public class XMLConfiguration extends Ba
                     listener.setFilters(VERBOSE_CLASSES);
                 }
                 ((StatusLogger) LOGGER).registerListener(listener);
+                for (String msg : messages) {
+                    LOGGER.error(msg);
+                }
             }
 
         } catch (SAXException domEx) {

Modified: 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java?rev=1370663&r1=1370662&r2=1370663&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
 (original)
+++ 
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
 Wed Aug  8 04:28:22 2012
@@ -55,6 +55,11 @@ public class Interpolator implements Str
         }
     }
 
+    public Interpolator() {
+        this.defaultLookup= new MapLookup(new HashMap<String, String>());
+        lookups.put("sys", new SystemPropertiesLookup());
+    }
+
      /**
      * Resolves the specified variable. This implementation will try to extract
      * a variable prefix from the given variable name (the first colon (':') is

Copied: 
logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationPropsTest.java
 (from r1370375, 
logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java)
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationPropsTest.java?p2=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationPropsTest.java&p1=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java&r1=1370375&r2=1370663&rev=1370663&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java
 (original)
+++ 
logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationPropsTest.java
 Wed Aug  8 04:28:22 2012
@@ -35,31 +35,18 @@ import java.io.FileOutputStream;
 import java.util.Iterator;
 import java.util.Map;
 
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
 
 /**
  *
  */
-public class XMLConfigurationTest {
+public class XMLConfigurationPropsTest {
 
-    private static final String CONFIG = "log4j-test1.xml";
+    private static final String CONFIG = "log4j-props.xml";
     private static final String LOGFILE = "target/test.log";
 
     @BeforeClass
     public static void setupClass() {
-        
System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
-        LoggerContext ctx = (LoggerContext) LogManager.getContext();
-        Configuration config = ctx.getConfiguration();
-        if (config instanceof XMLConfiguration) {
-            String name = ((XMLConfiguration) config).getName();
-            if (name == null || !name.equals("XMLConfigTest")) {
-                ctx.reconfigure();
-            }
-        } else {
-            ctx.reconfigure();
-        }
     }
 
     @AfterClass
@@ -71,48 +58,42 @@ public class XMLConfigurationTest {
     }
 
     @Test
-    public void testLogger() {
-        Logger logger = 
LogManager.getLogger("org.apache.logging.log4j.test1.Test");
-        assertTrue(logger instanceof org.apache.logging.log4j.core.Logger);
-        org.apache.logging.log4j.core.Logger l = 
(org.apache.logging.log4j.core.Logger) logger;
-        assertEquals(Level.DEBUG, l.getLevel());
-        int filterCount = l.filterCount();
-        assertTrue("number of filters - " + filterCount, filterCount == 1);
-        Iterator<Filter> iter = l.getFilters();
-        Filter filter = iter.next();
-        assertTrue(filter instanceof ThreadContextMapFilter);
-        Map<String, Appender> appenders = l.getAppenders();
-        assertNotNull(appenders);
-        assertTrue("number of appenders = " + appenders.size(), 
appenders.size() == 1);
-        Appender a = appenders.get("STDOUT");
-        assertNotNull(a);
-        assertEquals(a.getName(), "STDOUT");
-    }
-
-    public void testConfiguredAppenders() {
+    public void testNoProps() {
+        
System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
         LoggerContext ctx = (LoggerContext) LogManager.getContext();
-        Configuration c = ctx.getConfiguration();
-        Map<String, Appender> apps = c.getAppenders();
-        assertNotNull(apps);
-        assertEquals(apps.size(), 3);
+        ctx.reconfigure();
+        Configuration config = ctx.getConfiguration();
+        assertTrue("Configuration is not an XMLConfiguration", config 
instanceof XMLConfiguration);
     }
 
     @Test
-    public void logToFile() throws Exception {
-        FileOutputStream fos = new FileOutputStream(LOGFILE, false);
-        fos.flush();
-        fos.close();
-        Logger logger = 
LogManager.getLogger("org.apache.logging.log4j.test2.Test");
-        logger.debug("This is a test");
-        DataInputStream is = new DataInputStream(new BufferedInputStream(new 
FileInputStream(LOGFILE)));
-        int count = 0;
-        String str = "";
-        while (is.available() != 0) {
-            str = is.readLine();
-            ++count;
+    public void testWithConfigProp() {
+        
System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
+        System.setProperty("log4j.level", "debug");
+        try {
+            LoggerContext ctx = (LoggerContext) LogManager.getContext();
+            ctx.reconfigure();
+            Configuration config = ctx.getConfiguration();
+            assertTrue("Configuration is not an XMLConfiguration", config 
instanceof XMLConfiguration);
+        } finally {
+            System.clearProperty("log4j.level");
+
         }
-        assertTrue("Incorrect count " + count, count == 1);
-        assertTrue("Bad data", str.endsWith("This is a test"));
     }
 
+    @Test
+    public void testWithProps() {
+        
System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
+        System.setProperty("log4j.level", "debug");
+        System.setProperty("log.level", "debug");
+        try {
+            LoggerContext ctx = (LoggerContext) LogManager.getContext();
+            ctx.reconfigure();
+            Configuration config = ctx.getConfiguration();
+            assertTrue("Configuration is not an XMLConfiguration", config 
instanceof XMLConfiguration);
+        } finally {
+            System.clearProperty("log4j.level");
+            System.clearProperty("log.level");
+        }
+    }
 }

Added: logging/log4j/log4j2/trunk/core/src/test/resources/log4j-props.xml
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/resources/log4j-props.xml?rev=1370663&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/resources/log4j-props.xml (added)
+++ logging/log4j/log4j2/trunk/core/src/test/resources/log4j-props.xml Wed Aug  
8 04:28:22 2012
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration status="${sys:log4j.level}" strict="false" name="DSI" 
packages="com.terradue.dsione">
+  <appenders>
+    <Console name="Console" target="SYSTEM_OUT">
+      <PatternLayout pattern="[%-5level] %msg%n" />
+    </Console>
+  </appenders>
+
+  <loggers>
+    <root level="${sys:log.level}">
+      <appender-ref ref="Console" />
+    </root>
+  </loggers>
+</configuration>
\ No newline at end of file

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1370663&r1=1370662&r2=1370663&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Wed Aug  8 04:28:22 2012
@@ -22,6 +22,11 @@
   </properties>
 
   <body>
+    <release version="2.0-alpha2" date="TBD" description="Bug fixes and minor 
enhancements">
+      <action issue="LOG4J2-65" dev="rgoers">
+        Allow variable substitution on the configuration attributes and on the 
root log level.
+      </action>
+    </release>
     <release version="2.0-alpha1" date="2012-07-29" description="Rewrite of 
Log4j">
       <action issue="LOG4J2-60" dev="rgoers" type="add" due-to="Shane Kelly">
         Added ability to filter on the appender-ref by adding either a level 
or a filter.


Reply via email to