问题描述
我的Tomcat访问日志当前被来自负载均衡器的运行状况检查请求弄乱了,因此很难真正了解正在发生的事情.例如,使用GoAccess,我可以看到一些令人误解的统计信息:
My Tomcat access log is currently cluttered with health check requests from a load balancer, so it's rather hard to actually understand what's going on. For example, using GoAccess I can see some misleading statistics:
Hits h% Vis. v% Bandwidth Mtd Proto Data
----- ------ ---- ----- ----------- --- -------- ----
46221 81.20% 2 0.02% 30.72 MiB GET HTTP/1.1 /geoserver/index.html
16 0.03% 1 0.01% 50.23 KiB GET HTTP/1.1 /geoserver/wms?SERVICE=WMS&VERSION=1.1.0&REQUEST=GetMap&FORMAT=image/jpeg.
16 0.03% 1 0.01% 338.80 KiB GET HTTP/1.1 /geoserver/wms?SERVICE=WMS&VERSION=1.1.0&REQUEST=GetMap&FORMAT=image/png.
该日志是使用Tomcat的标准访问日志阀一个>.该阀门应该具有一个参数 conditionUnless
,我尝试使用该参数来消除对 index.html
发出的所有请求(在此处进行健康检查),所以我可以安全地过滤掉所有这些).
The log is created using Tomcat's standard Access Log Valve. The valve is supposed to have a parameter, conditionUnless
, which I try to use in order to get rid of all those requests being made to index.html
(that's where health check goes, so I can safely filter out all of them).
根据文档,条件除非
:
但是我不知道如何使用过滤器过滤出所有对 index.html
的请求,并在其中进行标记.显然, server.xml
中的以下内容是不够的:
But I can't figure out how to use Filters to filter out all requests to index.html
and flag them in some what. Obviously, the following in server.xml
is not enough:
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="/var/log/tomcat8/accesslogs"
prefix="node1" suffix=".log"
pattern="combined"
renameOnRotate="true"
conditionUnless="index.html" />
如何排除对 index.html
的所有请求?
How can I exclude all requests to index.html
?
推荐答案
您需要创建一个在Tomcat组中建议的过滤器,该过滤器将属性添加为 doLog
You need to create a filter as suggested in tomcat group that adds an attribute as doLog
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("doLog"), "true");
chain.doFilter(request, response);
}
public void destroy() {
this.filterConfig = null;
}
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
}
,然后使用 conditionIf
conditionIf="doLog"
并将过滤器添加到web.xml:
and add filter to web.xml:
<filter>
<filter-name>LoggingFilter</filter-name>
<filter-class>com.yourpackage.LoggingFilter</filter-class>
<init-param>
<param-name>logParam</param-name>
<param-value>doLog</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LoggingFilter</filter-name>
<url-pattern>/geoserver/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<filter-mapping>
这篇关于从Tomcat的日志中排除某些请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!