Log4j and vaadin application

Hello, I’m newbie in vaadin and I have a problem with logging. I want to show login of user in NDC in log4j. But there is a little problem. When I push the login NDC.push(connect_login); some time later vaadin changes the thread and new thread doesn’t have any login in logs: example:

[04.05.11]
 [12:40:30]
 DEBUG [http-8080-exec-6]
 [John]
 TypeSectionForm  - creating in process
[04.05.11]
 [12:40:30]
 DEBUG [http-8080-exec-6]
 [John]
 VaadinRuntimeFormBuilder  - Finish building document's types
[04.05.11]
 [12:40:38]
 DEBUG [http-8080-exec-8]
 [] VaadinformbuilderApplication  - setCardTabVisible: Agreement
[04.05.11]
 [12:40:39]
 DEBUG [http-8080-exec-8]
 [] VaadinformbuilderApplication  - Agreement_OS4.xml exists
[04.05.11]
 [12:40:39]
 DEBUG [http-8080-exec-8]
 [] VaadinformbuilderApplication  - Agreement_OS4.xml can be read

but http-8080-exec-8 is the same application! Can you help me to understand It? (and sorry for my english) thx.

Hi,

This is not really a Vaadin issue, but a general HTTP application one; it’s entirely possible (and, in fact, probable) that each HTTP request will be processed on a different thread - if that were not the case, then there would have to be as many threads as there were concurrent clients, which would perform very well! Moreover, each thread will be re-used by different requests (and users), so it’s important to remove the NDC after the request as well.

What you want to do is to set the NDC at the start of the request, and remove it at the end. This is actually very easy to do - just implement HttpServletRequestListener on the Application, and push/pop in the request start/stop methods. Here’s a quick hack example

public class DummyApp extends Application implements HttpServletRequestListener {
  protected String userName = "unknown-user";
  [...]
 normal app init method here [...]


  @Override
  public void setUser(Object user) {
    super.setUser(user);    
    userName = String.valueOf(user);
  }

  public void onRequestStart(HttpServletRequest request, HttpServletResponse response) {
    NDC.push(userName);
  }

  public void onRequestEnd(HttpServletRequest request, HttpServletResponse response) {
    NDC.pop();
  }
}

Hope that helps,

Cheers,

Charles.

It looks work! You saved me. I started logging userName to each message)
Thank you very much!