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 "Ebersole, Steven" <st...@vignette.com> on 2003/04/03 16:13:02 UTC

MDC in J2EE environment

There is certain information which is thread contextual which I would like
to include into log4j's MDC to be available for logging.  One of these, for
example, is the currently executing user.  My architecture is such that all
requests come through a layer of stateless session EJBs.  Now these EJBs can
make calls into other session EJBs in order to fulfill their use-case:

public class SessionBeanA
...
{
    ...
    public void executeUseCase()
    {
        ... // Do some work
        SessionBeanB sessionBeanB = ...; // Lookup SessionBeanB
        sessionBeanB.executeSomeRelatedUseCase();
        ... // Do some more work
    }
}

public class SessionBeanB
...
{
    ...
    public void executeRelatedUseCase()
    {
        ... // Do something
    }
}

The typical usage of MDC seems to be:
1) put vars into MDC
2) do your work
3) clean up MDC

But if I apply this usage to the scenario above, when
SessionBeanB.executeRelatedUseCase() cleans up the MDC, the information
would no longer be contained in the MDC for LoggingEvents generated within
the "Do some more work" section of SessionBeanA.executeUseCase().

I run weblogic 6.1, which unfortunately does not have support for "call
interceptors" to know when a user context has been bound to a thread.
Otherwise, I could simply setup MDC when a "session" is begun and clean up
the MDC when the session ends.  The only way around this I have been able to
think of is to just always call MDC.put( "USER",
mySessionContext.getCallerPrincipal().getName() ) at the beginning of each
and every session bean method.  But I would not ever be able to clean up the
MDC because of this nesting described above.

Is this OK?  Or this there a better way to do this?



Steve Ebersole
IT Integration Engineer
Vignette Corporation 
Office: 512.741.4195
Mobile: 512.297.5438

Visit http://www.vignette.com

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