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.
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).
According to documentation, conditionUnless
:
Turns on conditional logging. If set, requests will be logged only if
ServletRequest.getAttribute()
isnull
. For example, if this value is set to junk, then a particular request will only be logged ifServletRequest.getAttribute("junk") == null
. The use of Filters is an easy way to set/unset the attribute in the ServletRequest on many different requests.
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" />
How can I exclude all requests to index.html
?
Tomcat access logging is enabled by modifying the server.xml file and uncommenting the Access Log Valve. In a default tomcat implementation, the access log valve section is located within the Host element. Uncommenting the entry will enable an access log that contains fields equivalent to a "common" log file format from Apache.
Here’s a few ways to exclude requests – by URL or visitor IP – from the Nginx access log. In my Nginx configs I usually have a location block like this for static resources. It makes sure correct caching headers are sent, but also turns off logging for static resources – both the access log and the error log when a 404 is returned:
For this, we'll register our logging filter using a FilterRegistrationBean such that it matches only the required URL patterns: 2.2. Rule-out Filter If we want to exclude URLs from executing the logging task, we can achieve this easily in two ways: For a new URL, ensure that it doesn't match the URL patterns used by the filter
Tomcat Log Types Embedded Tomcat stores two types of logs: The access logs keep the records of all the requests processed by the application. These logs can be used to track things like page hit counts and user session activity. In contrast, internal server logs will help us to troubleshoot any issues in our running application.
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;
}
}
and then check attribute name using 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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With