我的Tomcat访问日志当前充满了来自负载均衡器的运行状况检查请求,因此很难真正了解正在发生的事情。例如,使用GoAccess,我可以看到一些令人误解的统计信息:
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.
Run Code Online (Sandbox Code Playgroud)
该日志是使用Tomcat的标准Access Log Valve创建的。该阀门应该具有一个参数,conditionUnless
我尝试使用该参数来消除对这些请求的所有要求index.html
(这是运行状况检查的地方,因此我可以安全地过滤掉所有请求)。
根据文档,conditionUnless
:
打开条件日志记录。如果设置,请求将仅被记录
ServletRequest.getAttribute()
的null
。例如,如果将此值设置为垃圾,则仅在记录特定请求ServletRequest.getAttribute("junk") == null
。使用过滤器是在许多不同请求上设置/取消ServletRequest中的属性的简便方法。
但是我无法弄清楚如何使用过滤器过滤出所有请求index.html
并以某种形式标记它们。显然,仅此一项server.xml
是不够的:
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="/var/log/tomcat8/accesslogs"
prefix="node1" suffix=".log"
pattern="combined"
renameOnRotate="true"
conditionUnless="index.html" />
Run Code Online (Sandbox Code Playgroud)
如何排除所有对的请求index.html
?
您需要按照tomcat组中的建议创建一个过滤器,以将属性添加为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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用conditionIf检查属性名称
conditionIf="doLog"
Run Code Online (Sandbox Code Playgroud)
conditionIf
打开条件日志记录。如果设置,则仅当ServletRequest.getAttribute()不为null时,才记录请求。例如,如果此值设置为重要,则仅当ServletRequest.getAttribute(“ important”)!= null时才记录特定请求。使用过滤器是在许多不同请求上设置/取消ServletRequest中的属性的简便方法。
并将过滤器添加到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>
Run Code Online (Sandbox Code Playgroud)