You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ca...@apache.org on 2008/08/11 23:39:05 UTC

svn commit: r684948 - in /logging/log4j/trunk: src/changes/changes.xml src/main/java/org/apache/log4j/config/PropertySetter.java tests/input/filter1.properties tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java

Author: carnold
Date: Mon Aug 11 14:39:04 2008
New Revision: 684948

URL: http://svn.apache.org/viewvc?rev=684948&view=rev
Log:
Bug 36384: Configuring triggering/rolling policies should be supported through properties

Added:
    logging/log4j/trunk/tests/input/filter1.properties
Modified:
    logging/log4j/trunk/src/changes/changes.xml
    logging/log4j/trunk/src/main/java/org/apache/log4j/config/PropertySetter.java
    logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java

Modified: logging/log4j/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/changes/changes.xml?rev=684948&r1=684947&r2=684948&view=diff
==============================================================================
--- logging/log4j/trunk/src/changes/changes.xml (original)
+++ logging/log4j/trunk/src/changes/changes.xml Mon Aug 11 14:39:04 2008
@@ -46,6 +46,7 @@
        <action action="fix" issue="45299">Javadoc class index corrupted by JDBCAppender deprecation warning.</action> 
        <action action="fix" issue="43867">Improve warning message when log4j is accessed after unload by Tomcat.</action>
        <action action="fix" issue="45335">NullPointerException in NDC.remove after unload by Tomcat.</action>
+       <action action="fix" issue="36384">Configuring triggering/rolling policys should be supported through properties.</action>
     </release>
   
     <release version="1.2.15" date="2007-08-24" description="SyslogAppender enhancements, NTEventLogAppender and Maven build.">

Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/config/PropertySetter.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/config/PropertySetter.java?rev=684948&r1=684947&r2=684948&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/config/PropertySetter.java (original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/config/PropertySetter.java Mon Aug 11 14:39:04 2008
@@ -126,7 +126,29 @@
         key = key.substring(len);
         if ("layout".equals(key) && obj instanceof Appender) {
           continue;
-        }        
+        }
+        //
+        //   if the property type is an OptionHandler
+        //     (for example, triggeringPolicy of org.apache.log4j.rolling.RollingFileAppender)
+        PropertyDescriptor prop = getPropertyDescriptor(Introspector.decapitalize(key));
+        if (prop != null
+                && OptionHandler.class.isAssignableFrom(prop.getPropertyType())
+                && prop.getWriteMethod() != null) {
+            OptionHandler opt = (OptionHandler)
+                    OptionConverter.instantiateByKey(properties, prefix + key,
+                                  prop.getPropertyType(),
+                                  null);
+            PropertySetter setter = new PropertySetter(opt);
+            setter.setProperties(properties, prefix + key + ".");
+            try {
+                prop.getWriteMethod().invoke(this.obj, new Object[] { opt });
+            } catch(Exception ex) {
+                LogLog.warn("Failed to set property [" + key +
+                            "] to value \"" + value + "\". ", ex);
+            }
+            continue;
+        }
+
         setProperty(key, value);
       }
     }

Added: logging/log4j/trunk/tests/input/filter1.properties
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/tests/input/filter1.properties?rev=684948&view=auto
==============================================================================
--- logging/log4j/trunk/tests/input/filter1.properties (added)
+++ logging/log4j/trunk/tests/input/filter1.properties Mon Aug 11 14:39:04 2008
@@ -0,0 +1,32 @@
+# 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.
+
+log4j.appender.ROLLING=org.apache.log4j.PropertyConfiguratorTest$RollingFileAppender
+log4j.appender.ROLLING.append=false
+log4j.appender.ROLLING.rollingPolicy=org.apache.log4j.PropertyConfiguratorTest$FixedWindowRollingPolicy
+log4j.appender.ROLLING.rollingPolicy.activeFileName=filterBase-test1.log
+log4j.appender.ROLLING.rollingPolicy.fileNamePattern=filterBased-test1.%i
+log4j.appender.ROLLING.rollingPolicy.minIndex=0
+log4j.appender.ROLLING.triggeringPolicy=org.apache.log4j.PropertyConfiguratorTest$FilterBasedTriggeringPolicy
+log4j.appender.ROLLING.triggeringPolicy.filter=org.apache.log4j.varia.LevelRangeFilter
+log4j.appender.ROLLING.triggeringPolicy.filter.levelMin=info
+log4j.appender.ROLLING.layout=org.apache.log4j.PatternLayout
+log4j.appender.ROLLING.layout.ConversionPattern=%m%n
+log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
+log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
+log4j.appender.CONSOLE.layout.ConversionPattern=%m%n
+log4j.logger.org.apache.log4j.PropertyConfiguratorTest=debug, ROLLING
+log4j.additivity.org.apache.log4j.rolling.FilterBasedRollingTest=false
+log4j.roolLogger=info, CONSOLE

Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java?rev=684948&r1=684947&r2=684948&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java (original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java Mon Aug 11 14:39:04 2008
@@ -24,6 +24,11 @@
 import java.net.URL;
 import java.util.Properties;
 
+import org.apache.log4j.spi.OptionHandler;
+import org.apache.log4j.spi.Filter;
+import org.apache.log4j.spi.LoggingEvent;
+import org.apache.log4j.varia.LevelRangeFilter;
+
 /**
  * Test property configurator.
  *
@@ -98,4 +103,168 @@
         LogManager.resetConfiguration();
     }
 
+
+    /**
+     * Mock definition of org.apache.log4j.rolling.RollingPolicy
+     * from extras companion.
+     */
+    public static class RollingPolicy implements OptionHandler {
+        private boolean activated = false;
+
+        public RollingPolicy() {
+
+        }
+        public void activateOptions() {
+            activated = true;
+        }
+
+        public final boolean isActivated() {
+            return activated;
+        }
+
+    }
+
+    /**
+     * Mock definition of FixedWindowRollingPolicy from extras companion.
+     */
+    public static final class FixedWindowRollingPolicy extends RollingPolicy {
+        private String activeFileName;
+        private String fileNamePattern;
+        private int minIndex;
+
+        public FixedWindowRollingPolicy() {
+            minIndex = -1;
+        }
+
+        public String getActiveFileName() {
+            return activeFileName;
+        }
+        public void setActiveFileName(final String val) {
+            activeFileName = val;
+        }
+
+        public String getFileNamePattern() {
+            return fileNamePattern;
+        }
+        public void setFileNamePattern(final String val) {
+            fileNamePattern = val;
+        }
+
+        public int getMinIndex() {
+            return minIndex;
+        }
+
+        public void setMinIndex(final int val) {
+            minIndex = val;
+        }
+    }
+
+    /**
+     * Mock definition of TriggeringPolicy from extras companion.
+     */
+    public static class TriggeringPolicy implements OptionHandler {
+        private boolean activated = false;
+
+        public TriggeringPolicy() {
+
+        }
+        public void activateOptions() {
+            activated = true;
+        }
+
+        public final boolean isActivated() {
+            return activated;
+        }
+
+    }
+
+    /**
+     * Mock definition of FilterBasedTriggeringPolicy from extras companion.
+     */
+    public static final class FilterBasedTriggeringPolicy extends TriggeringPolicy {
+        private Filter filter;
+        public FilterBasedTriggeringPolicy() {
+        }
+
+        public void setFilter(final Filter val) {
+             filter = val;
+        }
+
+        public Filter getFilter() {
+            return filter;
+
+        }
+    }
+
+    /**
+     * Mock definition of org.apache.log4j.rolling.RollingFileAppender
+     * from extras companion.
+     */
+    public static final class RollingFileAppender extends AppenderSkeleton {
+        private RollingPolicy rollingPolicy;
+        private TriggeringPolicy triggeringPolicy;
+        private boolean append;
+
+        public RollingFileAppender() {
+
+        }
+
+        public RollingPolicy getRollingPolicy() {
+            return rollingPolicy;
+        }
+
+        public void setRollingPolicy(final RollingPolicy policy) {
+            rollingPolicy = policy;
+        }
+
+        public TriggeringPolicy getTriggeringPolicy() {
+            return triggeringPolicy;
+        }
+
+        public void setTriggeringPolicy(final TriggeringPolicy policy) {
+            triggeringPolicy = policy;
+        }
+
+        public boolean getAppend() {
+            return append;
+        }
+
+        public void setAppend(boolean val) {
+            append = val;
+        }
+
+        public void close() {
+
+        }
+
+        public boolean requiresLayout() {
+            return true;
+        }
+
+        public void append(final LoggingEvent event) {
+
+        }
+    }
+
+    /**
+     * Test processing of nested objects, see bug 36384.
+     *
+     */
+    public void testNested() {
+        PropertyConfigurator.configure("input/filter1.properties");
+        RollingFileAppender rfa = (RollingFileAppender)
+                Logger.getLogger("org.apache.log4j.PropertyConfiguratorTest")
+                   .getAppender("ROLLING");
+        FixedWindowRollingPolicy rollingPolicy = (FixedWindowRollingPolicy) rfa.getRollingPolicy();
+        assertEquals("filterBase-test1.log", rollingPolicy.getActiveFileName());
+        assertEquals("filterBased-test1.%i", rollingPolicy.getFileNamePattern());
+        assertEquals(0, rollingPolicy.getMinIndex());
+        assertTrue(rollingPolicy.isActivated());
+        FilterBasedTriggeringPolicy triggeringPolicy =
+                (FilterBasedTriggeringPolicy) rfa.getTriggeringPolicy();
+        LevelRangeFilter filter = (LevelRangeFilter) triggeringPolicy.getFilter();
+        assertTrue(Level.INFO.equals(filter.getLevelMin()));
+        LogManager.resetConfiguration();
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org