不要记录 tomcat localhost_access_log 的特定 url 模式

do not log particular url pattern for tomcat localhost_access_log

我想知道是否有任何方法可以在 apache 访问日志中进行条件记录。 来自 $CATALINA_HOME/conf/server.xml

的 AccessLogValve
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access_log" suffix=".txt"
           pattern="%{X-Forwarded-For}i %h %l %u %t &quot;%r&quot; %s %b" />
    <Context path="" docBase="/usr/share/tomcat8/webapps/ROOT/www" reloadable="true" crossContext="true"/>

例如: 我不想记录传递敏感信息的 URL /email/somesensitve 信息。 有什么办法可以在 server.xml 或任何其他方式中指定它。

这就是我们在 apache httpd 中指定的方式 https://www.howtoforge.com/setenvif_apache2 SetEnvIf Request_URI "^/email/token$" dontlog

解决方案是在 Server.xml

中使用定义条件属性
<Valve className="org.apache.catalina.valves.FastCommonAccessLogValve" directory="logs" 
               prefix="access_log." suffix=".txt" pattern="common" resolveHosts="false" condition="donotLog" />

定义一个实现 Filter 的 RequestFilter

public class RequestFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    try {
        if ( request instanceof HttpServletRequest){
            HttpServletRequest httpServletRequest = (HttpServletRequest)request;
            String url = getFullURL(httpServletRequest);
            //initialize pattern only once
            if (pattern == null){
                String fPattern = filterConfig.getInitParameter("donotLogPattern");
                LOGGER.info("FilterPattern {}",fPattern);
                pattern = Pattern.compile(fPattern);
            }
            //If find a pttern then set do not log condition
            if (pattern.matcher(url).find()){
                LOGGER.info("Setting donotLog attribute");
                request.setAttribute("donotLog","true");
            }
        }
      ...

在 web.xml

中定义此 init-params
 <filter>
<filter-name>requestFilter</filter-name>
<filter-class>com.netgear.hms.config.RequestFilter</filter-class>
<init-param>
    <param-name>logParam</param-name>
    <param-value>donotLog</param-value>
</init-param>
<init-param>
    <param-name>donotLogPattern</param-name>
    <param-value>creditCard|passowrd</param-value>
</init-param>