You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by johnrock <jo...@yahoo.com> on 2009/05/26 00:20:27 UTC

How to configure Access logs to ignore images

I have set up an Access Log in Tomcat 6. Can someone tell me how to limit
access logging to only .jsp's and .html pages? 

I have read numerous posts and it is amazing how many responses there are to
similiar questions that all point to the 'condition' parameter
documentation...which I have thoroughly read. Checking the ServletRequest
for a null attribute, however, does not intuitively appear to be applicable
to filtering requests that are logged based on file extension.

Can this be done withouth re-writing your application to specifically pass
parameters just to  trigger logging? If so, could someone offer an example?

Thanks!

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="false"
            xmlValidation="false" xmlNamespaceAware="false">

        <Valve
className="org.apache.catalina.valves.FastCommonAccessLogValve"
directory="logs/access"
               prefix="access_log." suffix=".txt" pattern="common"
resolveHosts="false"/>

</Host>

-- 
View this message in context: http://www.nabble.com/How-to-configure-Access-logs-to-ignore-images-tp23714046p23714046.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: How to configure Access logs to ignore images

Posted by Arun Rajendran <nr...@gmail.com>.




Can anyone help us in blogging in particular IP logging in tomcat access
log?











johnrock wrote:
> 
> I have set up an Access Log in Tomcat 6. Can someone tell me how to limit
> access logging to only .jsp's and .html pages? 
> 
> I have read numerous posts and it is amazing how many responses there are
> to similiar questions that all point to the 'condition' parameter
> documentation...which I have thoroughly read. Checking the ServletRequest
> for a null attribute, however, does not intuitively appear to be
> applicable to filtering requests that are logged based on file extension.
> 
> Can this be done withouth re-writing your application to specifically pass
> parameters just to  trigger logging? If so, could someone offer an
> example?
> 
> Thanks!
> 
> <Host name="localhost"  appBase="webapps"
>             unpackWARs="true" autoDeploy="false"
>             xmlValidation="false" xmlNamespaceAware="false">
> 
>         <Valve
> className="org.apache.catalina.valves.FastCommonAccessLogValve"
> directory="logs/access"
>                prefix="access_log." suffix=".txt" pattern="common"
> resolveHosts="false"/>
> 
> </Host>
> 
> 

-- 
View this message in context: http://old.nabble.com/How-to-configure-Access-logs-to-ignore-images-tp23714046p31567407.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: How to configure Access logs to ignore images

Posted by johnrock <jo...@yahoo.com>.
Thanks Tim. Your bonus point approach is quite slick and tidy..I am quite
tempted to go that route. Of course that now begs me to overthink this and
wonder if there is any performance difference between having the web.xml 
filter-mapping do the logic or embedding that logic in the filter itself...


-- 
View this message in context: http://www.nabble.com/How-to-configure-Access-logs-to-ignore-images-tp23714046p23729600.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: How to configure Access logs to ignore images

Posted by Tim Funk <fu...@joedog.org>.
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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: How to configure Access logs to ignore images

Posted by johnrock <jo...@yahoo.com>.
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...
-- 
View this message in context: http://www.nabble.com/How-to-configure-Access-logs-to-ignore-images-tp23714046p23716977.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


RE: How to configure Access logs to ignore images

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: johnrock [mailto:johnpiser@yahoo.com]
> Subject: How to configure Access logs to ignore images
> 
> Checking the ServletRequest for a null attribute, however, 
> does not intuitively appear to be applicable to filtering
> requests that are logged based on file extension.

Perhaps you're interpreting this backwards.  You should be able to write a filter that gets called for whatever you don't want logged (*.jpg, *.css, etc.), and have that filter set some appropriate attribute in the request, and configure that attribute in the AccessLogValve.  Alternatively, have the filter called for everything, and set the attribute unless it's an item you do want logged.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


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