3 Replies Latest reply on Jun 4, 2008 8:49 PM by kfletcher2005

    How do I intercept or extend the seam logger?

    kfletcher2005

      How do I declare an interceptor for the seam logger and/or extend it so that it is transparent to developers who use the @Logger Log log.  The idea is to be able to log a transaction id or some sort of unique session information (such as the user id) without the developer having to add this information to the log statement so that when we troubleshoot a production issue, we can grep on the log for this unique id, and it will extract all associated logging so we can track where the user has been and where they were going when the error occured.  It is desirable to intercept or override these method calls log.info(), log.debug(), etc and pre-pend this unique identifier to the log statement for easy grepping.  I didn't see any docs or topics related to this issue.  Thanks Kent.

        • 1. Re: How do I intercept or extend the seam logger?
          pmuir

          Not possible AFAIK, but you could use something like a NDC/MDC with log4j.

          • 2. Re: How do I intercept or extend the seam logger?
            kfletcher2005

            NDC/MDC with log4j looks promising.  Thanks.

            • 3. Re: How do I intercept or extend the seam logger?
              kfletcher2005

              The final solution looks like the LoggingFilter built into seam.  You can add whatever you want to the MDC map from the request/session contexts.



              @Scope(ScopeType.APPLICATION)
              @Name("yourLoggingFilter")
              @BypassInterceptors
              @Filter(within = "org.jboss.seam.web.authenticationFilter")
              @Install(classDependencies = "org.apache.log4j.Logger", 
                    dependencies = "org.jboss.seam.security.identity", 
                    precedence = Install.BUILT_IN)
              public class YourLoggingFilter extends AbstractFilter
              {
              
                 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                       FilterChain filterChain) throws IOException, ServletException
                 {
                    HttpServletRequest req = ((HttpServletRequest) servletRequest);
                    for(Cookie cookie :req.getCookies() )
                    {
                       MDC.put(cookie.getName(), cookie.getValue() );
                    }
                    
                    HttpSession session = ((HttpServletRequest) servletRequest).getSession(false);
                    if (session != null)
                    {
                       Object attribute = session.getAttribute("org.jboss.seam.security.identity");
                       if (attribute instanceof Identity)
                       {
                          Identity identity = (Identity) attribute;
                          String username = identity.getUsername();
                          if (username != null)
                          {
                             /*
                              * the log4j output conversion pattern looks like...
                              * <param name="ConversionPattern" value="%d %-5r %-5p [%c] (%X{username} %X{remoteAddress}) %m%n"/>
                              */
                             MDC.put("username", username);
                          }
                       }
                       MDC.put("remoteAddress", servletRequest.getRemoteAddr());
                       MDC.put("whatever", "whatever");
                       MDC.put("locale", req.getLocale());         
                    }
              
                    filterChain.doFilter(servletRequest, servletResponse);
              
                    /*
                     * cleanup
                     */
                    MDC.remove("username");
                    MDC.remove("remoteAddress");
                    MDC.remove("whatever");
                    MDC.remove("locale");
                    
                    for(Cookie cookie :req.getCookies() )
                    {
                       MDC.remove(cookie.getName() );
                    }
                    
                  }
              
              }