You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by "Yu Xiang Xi (Maveo)" <yx...@Maveosystems.com> on 2003/10/22 10:32:27 UTC

Mapped Diagnostic Context in Struts Action Class

Hi, this is a combination question about both log4j and struts.

For the MDC is managed on a per thread basis, we can use it to log the user
IP address in the web application.
While, as I remember, struts action class is designed to be thread-safe.
If I try to log the user IP address in the execute () method of struts
action class, 

For example
public ActionForward execute(ActionMapping actionMapping, ActionForm
actionForm,  HttpServletRequest request, HttpServletResponse response)
throws Exception {
	   String username=session.getAttribute("user");
        MDC.put("userip",request.getRemoteAddr());
        logger.info(username+" login");
..
}

Will the unexpected case happen?

1. UserA from 192.168.0.1 login, and the MDC put 192.168.0.1 to the MDC
2. UserB from 192.168.0.2 login, and the MDC put 192.168.0.2 to the MDC(now
the userip in MDC  is updated)
3. UserA executed the code logger.info(username+" login");
4. UserA executed the code logger.info(username+" login");

so the log4j output maybe look like
[192.168.0.2] UserA login
[192.168.0.2] UserB login

Actually the assumption is
[192.168.0.1] UserA login
[192.168.0.2] UserB login

Best Regards
Xi Yuxiang

System Architect
Maveo Systems Limited - www.maveosystems.com

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org


Re: Mapped Diagnostic Context in Struts Action Class

Posted by Donald Larmee | ALTERthought <dl...@alterthought.com>.
I have done what you are attempting with no unexpected results... i.e., 
UserA maps to address 1 and UserB maps to address 2 just fine.

The ability for this to work correctly really lies more with the Thread 
strategy employed by your servlet engine, and less on Struts.... in order 
for this to work as designed you need to make sure that your Servlet engine 
is configured to use a multi-threading model (which is the out of the box 
default for most...)


At 04:32 AM 10/22/2003, Yu Xiang Xi  (Maveo) wrote:
>Hi, this is a combination question about both log4j and struts.
>
>For the MDC is managed on a per thread basis, we can use it to log the user
>IP address in the web application.
>While, as I remember, struts action class is designed to be thread-safe.
>If I try to log the user IP address in the execute () method of struts
>action class,
>
>For example
>public ActionForward execute(ActionMapping actionMapping, ActionForm
>actionForm,  HttpServletRequest request, HttpServletResponse response)
>throws Exception {
>            String username=session.getAttribute("user");
>         MDC.put("userip",request.getRemoteAddr());
>         logger.info(username+" login");
>..
>}
>
>Will the unexpected case happen?
>
>1. UserA from 192.168.0.1 login, and the MDC put 192.168.0.1 to the MDC
>2. UserB from 192.168.0.2 login, and the MDC put 192.168.0.2 to the MDC(now
>the userip in MDC  is updated)
>3. UserA executed the code logger.info(username+" login");
>4. UserA executed the code logger.info(username+" login");
>
>so the log4j output maybe look like
>[192.168.0.2] UserA login
>[192.168.0.2] UserB login
>
>Actually the assumption is
>[192.168.0.1] UserA login
>[192.168.0.2] UserB login
>
>Best Regards
>Xi Yuxiang
>
>System Architect
>Maveo Systems Limited - www.maveosystems.com
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: log4j-user-help@jakarta.apache.org


_______________________________________

   Donald H. Larmee
   ALTERthought, Inc.
   804.301.8867 (c)

   www.alterthought.com
_______________________________________ 


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org