Tomcat6 - 如何通过log4j或JULI获取会话参数?

时间:2013-04-11 02:36:45

标签: tomcat logging log4j

我遇到的问题是,部署的Web应用程序(没有源代码或任何引用)似乎遇到了某些会话参数的问题。我不知道如何对该应用程序进行逆向工程,所以我觉得我能展示这一事件的唯一方法是使用log4j或tomcat自己的JULI工具来获取会话参数列表。我读过有关Wrox专业Apache Tomcat 6的内容,坦白说在这方面没有太多帮助......

有关使用log4j或JULI的方向的任何提示吗?

非常感谢提前。

1 个答案:

答案 0 :(得分:1)

您可以添加过滤器以将会话属性列表打印到Log4J,如下所示:

public class SessionAttributesLoggingFilter implements Filter {

    private Logger log = Logger.getLogger(this.getClass());

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (request instanceof HttpServletRequest) {
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            HttpSession session = httpRequest.getSession();

            Enumeration<String> sessionAttributeNames = session.getAttributeNames();
            log.info("Session attributes before request ----------------------->");
            while (sessionAttributeNames.hasMoreElements()) {
                String attr = sessionAttributeNames.nextElement();
                log.info(" "+attr + " = " + session.getAttribute(attr));
            }
            chain.doFilter(request, response);
            sessionAttributeNames = session.getAttributeNames();
            log.info("Session attributes after request ----------------------->");
            while (sessionAttributeNames.hasMoreElements()) {
                String attr = sessionAttributeNames.nextElement();
                log.info(" "+attr + " = " + session.getAttribute(attr));
            }
        } else {
            chain.doFilter(request, response);
        }
    }

    public void init(FilterConfig arg0) throws ServletException {
        log.info("Logging session attributes ...");
    }

    public void destroy() {}
} 

将映射添加到web.xml并重新启动应用程序:

<filter>
    <filter-name>sessionLogging</filter-name>
    <filter-class>PACKAGENAME.SessionAttributesLoggingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>sessionLogging</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>