gpt4 book ai didi

tomcat - 从 Tomcat 的日志中排除某些请求

转载 作者:行者123 更新时间:2023-11-28 21:45:50 24 4
gpt4 key购买 nike

我的 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.

日志是使用 Tomcat 的标准创建的 Access Log Valve .阀门应该有一个参数,conditionUnless,我尝试使用它来摆脱所有对 index.html 的请求(这是健康检查的地方去,所以我可以安全地过滤掉所有这些)。

根据文档,conditionUnless:

Turns on conditional logging. If set, requests will be logged only if ServletRequest.getAttribute() is null. For example, if this value is set to junk, then a particular request will only be logged if ServletRequest.getAttribute("junk") == null. The use of Filters is an easy way to set/unset the attribute in the ServletRequest on many different requests.

但我不知道如何使用过滤器来过滤掉对 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" />

如何排除对 index.html 的所有请求?

最佳答案

您需要创建一个 filter as suggested in tomcat group将属性添加为 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"

conditionIf
Turns on conditional logging. If set, requests will be logged only if ServletRequest.getAttribute() is not null. For example, if this value is set to important, then a particular request will only be logged if ServletRequest.getAttribute("important") != null. The use of Filters is an easy way to set/unset the attribute in the ServletRequest on many different requests.

并在 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 - 从 Tomcat 的日志中排除某些请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53740326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com