Split "condition" attribute of AccessLogValve into two, "conditionIf" and "conditionU...
authorkkolinko <kkolinko@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 4 Aug 2011 03:53:27 +0000 (03:53 +0000)
committerkkolinko <kkolinko@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 4 Aug 2011 03:53:27 +0000 (03:53 +0000)
Implement conditional logging that logs only if a request attribute is present.
The old attribute "condition" is provided as well, for backwards compatibility, and is equal to "conditionUnless".

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1153742 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/valves/AccessLogValve.java
java/org/apache/catalina/valves/mbeans-descriptors.xml
webapps/docs/changelog.xml
webapps/docs/config/valve.xml

index c7570aa..1490e34 100644 (file)
@@ -135,9 +135,13 @@ import org.apache.tomcat.util.buf.B2CConverter;
  * 
  * <p>
  * Conditional logging is also supported. This can be done with the
- * <code>condition</code> property.
- * If the value returned from ServletRequest.getAttribute(condition)
+ * <code>conditionUnless</code> and <code>conditionIf</code> properties.
+ * If the value returned from ServletRequest.getAttribute(conditionUnless)
  * yields a non-null value, the logging will be skipped.
+ * If the value returned from ServletRequest.getAttribute(conditionIf)
+ * yields the null value, the logging will be skipped.
+ * The <code>condition</code> attribute is synonym for
+ * <code>conditionUnless</code> and is provided for backwards compatibility.
  * </p>
  * 
  * <p>
@@ -524,13 +528,19 @@ public class AccessLogValve extends ValveBase implements AccessLog {
      * agent renames the log file so we can automagically recreate it.
      */
     private boolean checkExists = false;
-    
-    
+
+
     /**
-     * Are we doing conditional logging. default false.
+     * Are we doing conditional logging. default null.
+     * It is the value of <code>conditionUnless</code> property.
      */
     protected String condition = null;
 
+    /**
+     * Are we doing conditional logging. default null.
+     * It is the value of <code>conditionIf</code> property.
+     */
+    protected String conditionIf = null;
 
     /**
      * Date format to place in log file name. Use at your own risk!
@@ -788,6 +798,46 @@ public class AccessLogValve extends ValveBase implements AccessLog {
 
 
     /**
+     * Return whether the attribute name to look for when
+     * performing conditional logging. If null, every
+     * request is logged.
+     */
+    public String getConditionUnless() {
+        return getCondition();
+    }
+
+
+    /**
+     * Set the ServletRequest.attribute to look for to perform
+     * conditional logging. Set to null to log everything.
+     *
+     * @param condition Set to null to log everything
+     */
+    public void setConditionUnless(String condition) {
+        setCondition(condition);
+    }
+
+    /**
+     * Return whether the attribute name to look for when
+     * performing conditional logging. If null, every
+     * request is logged.
+     */
+    public String getConditionIf() {
+        return conditionIf;
+    }
+
+
+    /**
+     * Set the ServletRequest.attribute to look for to perform
+     * conditional logging. Set to null to log everything.
+     *
+     * @param condition Set to null to log everything
+     */
+    public void setConditionIf(String condition) {
+        this.conditionIf = condition;
+    }
+
+    /**
      *  Return the date format date based log rotation.
      */
     public String getFileDateFormat() {
@@ -882,9 +932,11 @@ public class AccessLogValve extends ValveBase implements AccessLog {
     
     @Override
     public void log(Request request, Response response, long time) {
-        if (!getState().isAvailable() || !getEnabled() ||
-                logElements == null || condition != null
-                && null != request.getRequest().getAttribute(condition)) {
+        if (!getState().isAvailable() || !getEnabled() || logElements == null
+                || condition != null
+                && null != request.getRequest().getAttribute(condition)
+                || conditionIf != null
+                && null == request.getRequest().getAttribute(conditionIf)) {
             return;
         }
 
index f5112ad..7b0103b 100644 (file)
                writeable="false"/>
 
     <attribute name="condition"
+               description="The value to look for conditional logging. The same as conditionUnless."
+               type="java.lang.String"/>
+
+    <attribute name="conditionIf"
+               description="The value to look for conditional logging."
+               type="java.lang.String"/>
+
+    <attribute name="conditionUnless"
                description="The value to look for conditional logging."
                type="java.lang.String"/>
 
                writeable="false"/>
 
     <attribute name="condition"
+               description="The value to look for conditional logging. The same as conditionUnless."
+               type="java.lang.String"/>
+
+    <attribute name="conditionIf"
+               description="The value to look for conditional logging."
+               type="java.lang.String"/>
+
+    <attribute name="conditionUnless"
                description="The value to look for conditional logging."
                type="java.lang.String"/>
 
index ac7e043..5d1036f 100644 (file)
         Make sure committed connection is returned to the pool if datasource is 
         enabled. (kfujino)
       </fix>
+      <add>
+        Split <code>condition</code> attribute of AccessLogValve into two,
+        <code>conditionIf</code> and <code>conditionUnless</code>. Implement
+        conditional logging that logs only if a request attribute is present.
+        (kkolinko)
+      </add>
     </changelog>
   </subsection>
   <subsection name="Coyote">
index 6af03e6..3272733 100644 (file)
         </p>
       </attribute>
 
-      <attribute name="condition" required="false">
+      <attribute name="conditionIf" required="false">
+        <p>Turns on conditional logging. If set, requests will be
+           logged only if <code>ServletRequest.getAttribute()</code> is
+           not null. For example, if this value is set to
+           <code>important</code>, then a particular request will only be logged
+           if <code>ServletRequest.getAttribute("important") != null</code>.
+           The use of Filters is an easy way to set/unset the attribute
+           in the ServletRequest on many different requests.
+        </p>
+      </attribute>
+
+      <attribute name="conditionUnless" required="false">
         <p>Turns on conditional logging. If set, requests will be
            logged only if <code>ServletRequest.getAttribute()</code> is
            null. For example, if this value is set to
         </p>
       </attribute>
 
+      <attribute name="condition" required="false">
+        <p>The same as <code>conditionUnless</code>. This attribute is
+           provided for backwards compatibility.
+        </p>
+      </attribute>
+
       <attribute name="fileDateFormat" required="false">
         <p>Allows a customized date format in the access log file name.
            The date format also decides how often the file is rotated.
         </p>
       </attribute>
 
-      <attribute name="condition" required="false">
+      <attribute name="conditionIf" required="false">
+        <p>Turns on conditional logging. If set, requests will be
+           logged only if <code>ServletRequest.getAttribute()</code> is
+           not null. For example, if this value is set to
+           <code>important</code>, then a particular request will only be logged
+           if <code>ServletRequest.getAttribute("important") != null</code>.
+           The use of Filters is an easy way to set/unset the attribute
+           in the ServletRequest on many different requests.
+        </p>
+      </attribute>
+
+      <attribute name="conditionUnless" required="false">
         <p>Turns on conditional logging. If set, requests will be
            logged only if <code>ServletRequest.getAttribute()</code> is
            null. For example, if this value is set to
         </p>
       </attribute>
 
+      <attribute name="condition" required="false">
+        <p>The same as <code>conditionUnless</code>. This attribute is
+           provided for backwards compatibility.
+        </p>
+      </attribute>
+
       <attribute name="fileDateFormat" required="false">
         <p>Allows a customized date format in the access log file name.
            The date format also decides how often the file is rotated.