Announcement Announcement Module
No announcement yet.
Log4J implementation with unique user logging in web app Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Log4J implementation with unique user logging in web app

    Hi all,

    In my application, I am using spring mvc 3.0 and dependency injection for service/dao creation.

    Now I have to implement log4j in entire application (mvc/web module, service layer packed - a seperate jar, dao layer packed in another jar, web service clients - packed in a seperate jar )

    My question are,
    1) How to implement Log4J efficiently in my application across all the modules.
    2) I want to distingush loged information on user user/request besis. I had read about NDC of log4j. But do not know how to use efficiently without pussing data in NDC. Actually I am looking for a better approach like if I could dump threadLoacal Id with all logs without doing much chenge in code it would be better. Please suggest.


  • #2
    Any Help will be highly appreciated!!


    • #3
      If you use log4j getting the current thread isn't hard (that is even explained in the log4j documentation), for that you need to change your log4j pattern.

      %d [%t] %-5p %c - %m%n

      Will lead to logging including the current executing thread (the %t).


      • #4
        thats fine...but for more sophistication...suppose I want to give userId for logging what I will have to do?


        • #5
          UserId is already in session -> UserDetail Bean


          • #6
            UserId is already in session -> UserDetail Bean
            Log4j doesn't know anything about the environment (as it should). if you want more sophisticated logging, details you really have to use the NDC. Which, if done right, isn't that much work, either create a Filter or Interceptor to fill the NDC with the information you want. Also clear the NDC when the request finished processing.

            I suggest a search on the internet because there has been written enough about that.