6 Replies Latest reply on Jul 5, 2007 4:55 PM by Gavin King

    Using log4j's NDC

    Hari N Newbie

      Hi
      I want to use the NDC facility of Log4j in Seam so that I can make sense of the logs

      Is there any way to do this ? Any pointers will be greatly appreciated.

      Regards
      Hari

      PS : I tried importing NDC and then did a NDC.push , but this is not appearing in the log file

        • 1. Re: Using log4j's NDC
          Stuart Robertson Novice

          Seam's logger works much like Jakarta Commons Logger, delegating to an underlying provider. If you're setup to use log4j underneath, then you'll have access to NDC or MDC. So I'm not sure this is really a seam question - it's more of a general logging configuration one.

          • 2. Re: Using log4j's NDC
            Adam Talaat Newbie

            Try adding a servlet Filter, which manages the NDC per request:

            private static final AtomicLong requestCount = new AtomicLong(0L);

            public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws ServletException, IOException {

            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) resp;

            // Retrieve the HttpSession. Create a new one if it does not exist.
            HttpSession session = request.getSession(true);
            String sessionId = session.getId();

            try {
            NDC.push(sessionId + ";" + requestCount.getAndIncrement());

            chain.doFilter(request, response);
            }
            finally {
            log.debug("Finished servicing request.");
            NDC.pop();
            }
            }

            • 3. Re: Using log4j's NDC
              Eric Trautman Newbie

              For what it is worth, here is what I'm currently using with Seam 1.2.1 to add the Seam identity username to the log4j mapped diagnostic context:

              @Startup
              @Scope(ScopeType.APPLICATION)
              @Name("some.package.loggingContextFilter")
              @Intercept(InterceptionType.NEVER)
              @Install
              public class LoggingContextFilter extends AbstractFilter {
              
               /** Identifier for the username "diagnostic context". */
               public static final String USERNAME_CONTEXT = "username";
              
               public void doFilter(ServletRequest servletRequest,
               ServletResponse servletResponse,
               FilterChain filterChain)
               throws IOException, ServletException {
              
               if (servletRequest instanceof HttpServletRequest) {
               HttpServletRequest req = (HttpServletRequest) servletRequest;
               String path = req.getServletPath();
               if ((path != null) && (path.endsWith(".seam"))) {
               HttpSession session = req.getSession();
               Object o = session.getAttribute("org.jboss.seam.security.identity");
               if (o instanceof Identity) {
               Identity identity = (Identity) o;
               String username = identity.getUsername();
               if (username != null) {
               MDC.put(USERNAME_CONTEXT, username);
               }
               }
               }
               }
              
               filterChain.doFilter(servletRequest, servletResponse);
              
               MDC.remove(USERNAME_CONTEXT);
               }
              }
              


              You could do something similar with the NDC if you wanted.

              • 4. Re: Using log4j's NDC
                Gavin King Master

                 

                here is what I'm currently using with Seam 1.2.1 to add the Seam identity username to the log4j mapped diagnostic context


                Hey, thats a nice idea, want to submit this code to JIRA, and I will see about adding it to the Seam logging package?

                • 5. Re: Using log4j's NDC
                  Eric Trautman Newbie

                  Gavin,

                  At your suggestion, I created the following JIRA feature request:
                  http://jira.jboss.com/jira/browse/JBSEAM-1610

                  This is my first request, so please forgive (and correct) me if I did not submit the information properly.

                  Thanks,
                  Eric

                  • 6. Re: Using log4j's NDC
                    Gavin King Master

                    Great, thanks :)