不要为tomcat localhost_access_log记录特定的URL模式

时间:2019-05-07 02:07:14

标签: apache tomcat tomcat8 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 / somesensitiveve 信息。 有什么办法可以在server.xml或其他任何方式中指定它。

这是我们可以在Apache httpd中指定的方式 https://www.howtoforge.com/setenvif_apache2 SetEnvIf Request_URI“ ^ / email / token $” dontlog

1 个答案:

答案 0 :(得分:0)

解决方案是在Server.xml中使用define condition属性

<Valve className="org.apache.catalina.valves.FastCommonAccessLogValve" directory="logs" 
               prefix="access_log." suffix=".txt" pattern="common" resolveHosts="false" condition="donotLog" />

定义实现过滤器的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>