【发布时间】:2014-11-02 06:20:31
【问题描述】:
我正在尝试从我的访问日志中过滤掉任何图像/css/js 请求。我创建了一个过滤器 (LoggingFilter) 并在 server.xml 中设置了 conditionUnless 属性,但我必须缺少一个步骤,因为对 image/css/js 文件的请求仍然显示在日志中。有人可以提示我缺少什么吗?
这是代码 -
LoggingFilter.java
public class LoggingFilter implements Filter
{
@Override
public void init(FilterConfig filterConfig) throws ServletException
{
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
chain.doFilter(request, response);
if(response.getContentType() != null)
{
if (response.getContentType().contains("image") ||
response.getContentType().contains("css") ||
response.getContentType().contains("javascript"))
{
request.setAttribute("doNotLog", "noLog");
}
}
}
@Override
public void destroy()
{
}
}
这是我在 web.xml 中的内容
<filter>
<filter-name>LoggingFilter</filter-name>
<filter-class>com.shop.famos.filters.LoggingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoggingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在我的 tomcat 设置中 -
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" conditionUnless="doNotLog" pattern="common"/>
顺便说一句,我正在使用 tomcat 7
另外,想提一下我确实看到了这篇文章Tomcat 7: Filter access log,它说必须继承“org.apache.catalina.valves.AccessLogValve”才能实现更具体的过滤器。这是唯一的方法吗?或者可以使用我上面的过滤器来实现吗?
【问题讨论】:
标签: tomcat