Simpler version ... (Writing this without enough coffee in my system so the spirit is right, the logic might be iffy) - but in a nutshell

- You don't need to map to forward, error, include - just the incoming request
- You should be able to check the response type

doFilter(req, resp, chain) {
  chain.doFilter(req, resp);
  if (resp.getContentType().startsWith("image/")) {
    request.setAttribute("dlog","t");
  } else   if (resp.getContentType().startsWith("audio/")) {
    request.setAttribute("dlog","t");
  }
  /* ... */

}

<filter>
    <filter-name>LoggingFilter</filter-name>
    <filter-class>com.mg.filters.LoggingFilter</filter-class>
    <init-param>
       <param-name>logParam</param-name>
       <param-value>dLog</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>LoggingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


For bonus points - you could even do either of the following so its more configurable:
doFilter(req, resp, chain) {
  chain.doFilter(req, resp);
  String p = filterConfig.getInitParameter("pattern");
  if (resp.getContentType().matches(p)) {
    request.setAttribute("dlog","t");
  }
  /* ... */

}


Of course - you could go the codeless route and use
http://tuckey.org/urlrewrite/manual/3.0/ to set the attribute too.


-Tim

johnrock wrote:
Thanks for your help. I do understand now the concept. I am quite surprised
however how much code it actually took. Here is my filter configuration that
is working correctly.

This configuration basically filters out everything I don't want in my
access logs, including a couple of redundant frameset pages that I do not
want to count.

LoggingFilter.java is the filter class that adds a parameter to the request
object of any page it processes. The tomcat accesslogvalve then only logs
requests that do not have the dLog parameter:


LoggingFilter.java:

package com.mg.filters;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public final class LoggingFilter implements Filter {

      private FilterConfig filterConfig = null;

    public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
        throws IOException, ServletException {

request.setAttribute(filterConfig.getInitParameter("logParam"),"t"); chain.doFilter(request, response);
    }
    public void destroy() {
        this.filterConfig = null;
    }
    public void init(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;       
    }

}



Tomcat server.xml:
<Valve className="org.apache.catalina.valves.FastCommonAccessLogValve"
directory="logs"
               prefix="access_log." suffix=".txt" pattern="common"
resolveHosts="false" condition="dLog" />


web.xml:

<!-- The following url patterns will not be written to the access logs --> <filter> <filter-name>LoggingFilter</filter-name> <filter-class>com.mg.filters.LoggingFilter</filter-class> <init-param> <param-name>logParam</param-name> <param-value>dLog</param-value> </init-param> </filter> <filter-mapping> <filter-name>LoggingFilter</filter-name> <url-pattern>/images/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>
<filter-mapping> <filter-name>LoggingFilter</filter-name> <url-pattern>/css/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>
<filter-mapping> <filter-name>LoggingFilter</filter-name> <url-pattern>/js/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>
<filter-mapping> <filter-name>LoggingFilter</filter-name> <url-pattern>/audio/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>
<filter-mapping> <filter-name>LoggingFilter</filter-name> <url-pattern>/dwr/*</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping> <filter-mapping> <filter-name>LoggingFilter</filter-name> <url-pattern>/</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>
<filter-mapping> <filter-name>LoggingFilter</filter-name> <url-pattern>/index.html</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>
<filter-mapping> <filter-name>LoggingFilter</filter-name> <url-pattern>/talk.htm</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>
<filter-mapping> <filter-name>LoggingFilter</filter-name> <url-pattern>/core.htm</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

Any comments or suggestions on this approach? I am not sure if it is
neccessary to include all of those dispatcher elements, but it seems to me
like it would be necessary to be complete...


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to