NDC/MDC in log4j
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Nitin_Nair
Posted On:   Friday, May 2, 2003 04:44 AM

Hi, The log messages in my previous question were not very clear..I am putting my question again... I am working in a project where for an EJB environment for logging the messages we are using Log4j. Here we are using nested diagnostics contexts (NDC)/MDC to differentiate logging requests from different clients. The control flow in my application is as follows :- Servlet1 --> Servlet2 --> EJB [ 1... n] Also as usual there are bean to bean calls. The NDC.push(hostname) is being done in the "doPost(HttpServletRequest req, HttpServletResponse res)" of Servlet 1. Currently what is happening is, on using NDC/MDC, all the logger stmts in Servlet1 are getting printed prope   More>>

Hi,

The log messages in my previous question were not very clear..I am putting my question again...



I am working in a project where for an EJB environment for logging the messages we are using Log4j. Here we are using nested diagnostics contexts (NDC)/MDC to differentiate logging requests from different clients. The control flow in my application is as follows :-

Servlet1 --> Servlet2 --> EJB [ 1... n]

Also as usual there are bean to bean calls.

The NDC.push(hostname) is being done in the "doPost(HttpServletRequest req, HttpServletResponse res)" of Servlet 1.

Currently what is happening is, on using NDC/MDC, all the logger stmts in Servlet1 are getting printed properly with correct user name. But the moment control comes in Servelt2 onwards, the user info is getting missed out.Is there someting in particular which needs to be taken care of ??? Or How do we achieve this?

My log messages are something like this:



INFO - [NITIN.pune.tcs.co.in] [ExecuteThread: '11' for queue: 'default'] - Calling CreateRequest ("ServiceCall" , svcName) on the class RequestFactory

INFO - [NITIN.pune.tcs.co.in] [ExecuteThread: '11' for queue: 'default'] - Making a ervice call request inside CreateRequest(String request , String subject) of class RequestFactory

INFO - [NITIN.pune.tcs.co.in] [ExecuteThread: '11' for queue: 'default'] - Calling SendRequest (sreq,secure) on the class WebArchUtil

INFO - [] [ExecuteThread: '12' for queue: 'default'] - While creating ThreadSpecificContext, type is UTCS

INFO - [] [ExecuteThread: '12' for queue: 'default'] - In setBeanContext, type is UTCS

INFO - [] [ExecuteThread: '12' for queue: 'default'] - dataSource is weblogic.jdbc.common.internal.RmiDataSource@340272

INFO - [] [ExecuteThread: '12' for queue: 'default'] - DataSource is weblogic.jdbc.common.internal.RmiDataSource@340272

INFO - [] [ExecuteThread: '12' for queue: 'default'] - SQL Statement is select AuditFlg from BT_TBL_AUD where BT_Name = ?

INFO - [] [ExecuteThread: '12' for queue: 'default'] - SQL Statement is insert into Tbl_licagentdetails ( LICAgentNo ,LICAgentName ,LICAgentAddress ,Tbl_licagentdetails_ver ,DM_LstUpdDt ) VALUES ( ?,?,?,? , ? )

INFO - [NITIN.pune.tcs.co.in] [ExecuteThread: '11' for queue: 'default'] - The action to be performed is 0


Regards Nitin    <<Less

Re: NDC/MDC in log4j

Posted By:   Jeff_Drost  
Posted On:   Friday, May 2, 2003 06:25 AM

Both NDC and MDC store their info on the currently executing thread using java.lang.ThreadLocal. Inside the application server, it appears as though your request is being handed off from one thread to another.


Notice that you loose your NDC data at the exact same line in your log as the executing thread switching from ExecuteThread 11 to 12 - it then reappears when thread 11 is resumed.


I don't think there is really any good way to prevent this. Is your user authenticated? If so, you find out who the user is from Servlets and EJBs.


Check out HttpServletRequest.getRemoteUser() and EJBContext.getCallerPrincipal().


Buy the way.. what app server are you using? I'm somewhat surprised the second servlet is being executed on a different thread. You are using a RequestDispatcher, right? Are the servlets in the same context/webapp?

About | Sitemap | Contact