Tomcat 过滤器生成重复的 localhost.log 行 [英] Tomcat filter generates duplicate localhost.log lines

查看:72
本文介绍了Tomcat 过滤器生成重复的 localhost.log 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码,其中大部分是我继承的,运行良好,除了 System.out.println("Success") 在 localhost.log 中生成了很多(7-37,随机)相同的行运行时只有一个:

This code, most of which I inherited, runs fine except System.out.println("Success") generates a lot (7-37, random) of identical lines in localhost.log instead of just one when it runs:

Mar 1, 2011 8:49:47 AM org.apache.catalina.core.StandardWrapperValve invoke
Success
Mar 1, 2011 8:49:47 AM org.apache.catalina.core.StandardWrapperValve invoke
Success
Mar 1, 2011 8:49:47 AM org.apache.catalina.core.StandardWrapperValve invoke
Success

这是怎么回事??

public class SpecialFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc)
        throws IOException, ServletException {
    if (request instanceof HttpServletRequest) {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String mainID = httpRequest.getRemoteUser();
        String username = "";
        try {
            Cookie c[] = httpRequest.getCookies();
            if (c == null) {
                username = getID(mainID);  // method omitted, just executes a SQL query
            } else {
                boolean cookieFound = false;
                for (int i = 0; i < c.length; i++) {
                    if (c[i].getName().equals("mainCookie")) {
                        username = c[i].getValue();
                        cookieFound = true;
                        break;
                    }
                }
                if (cookieFound) {
                    System.out.println("Success");
                } else {
                    username = getID(mainID);
                }
            }
        } catch (SQLException e) {
            System.out.println("Error 1 " + e);
            throw new ServletException(error, e);
        }
        AuthRequestWrapper wrapper = new AuthRequestWrapper(httpRequest, username);
        fc.doFilter(wrapper, response);
    }
    else {
        throw new RuntimeException("request is not a valid httpRequest object.");
    }
}

}

推荐答案

您有多个记录器附加到同一个类.只需禁用您的根记录器.大多数情况下,这是重复日志语句的问题.干杯

You have multiple logger attached to the same class. Just disable your root logger. That is the problem with duplicate log statements most of the time. cheers

这篇关于Tomcat 过滤器生成重复的 localhost.log 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆