【问题标题】:do not log particular url pattern for tomcat localhost_access_log不要为 tomcat localhost_access_log 记录特定的 url 模式
【发布时间】:2019-09-24 15:25:56
【问题描述】:

我想知道是否有任何方法可以在 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$" 不要登录

【问题讨论】:

    标签: apache tomcat tomcat8 access-log


    【解决方案1】:

    解决办法是在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实现Filter

    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>
    

    【讨论】:

      猜你喜欢
      • 2015-10-31
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 2014-03-20
      • 1970-01-01
      相关资源
      最近更新 更多