-
1. Re: Using log4j's NDC
stu2 May 22, 2007 7:09 PM (in response to hvram)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
atalaat Jun 20, 2007 3:39 PM (in response to hvram)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
trautmane Jul 3, 2007 6:27 PM (in response to hvram)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 Jul 5, 2007 1:32 PM (in response to hvram)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
trautmane Jul 5, 2007 4:48 PM (in response to hvram)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 -