On May 31, 2010, at 3:28 PM, Thorbjørn Ravn Andersen wrote:

> Den 30/05/10 23.12, Curt Arnold skrev:
>> I don't have this in code or in the JIRA, but I have mentioned in recent 
>> threads the idea of a user-supplied context object in logging calls. 
>> Currently log4j has a thread associated context (the MDC and NDC) and there 
>> are JVM level context (line ending separator), but there is no concept of a 
>> user-supplied context unless embedded in the message parameter.
>> In this case, the logging call is operating in the "context" of the servlet 
>> request, and you could do pass the servlet as the user-context object.  A 
>> servlet appender could check if the user context object was a Servlet and if 
>> so delegate to its log method.  We could also add patterns for %ipaddr, 
>> %ipport, etc, that would attempt to recognize the user-context object and 
>> extract that info if it could recognize the type.
>>   
> I am unsure of what you describe. Could you write some pseudocode showing 
> what you mean?
> 

I'm working way below the client API at the moment, but the general idea is 
that in addition to MDC and NDC (aka the thread-associated context), the stack 
trace (aka the caller context), you can provide context with an explicit 
context parameter on the logging call. 

If the current logj4 API was extended to add user-supplied context, you'd have:

Logger.info(Object message, Throwable thrown, Object context);

In the context of a Servlet, you could end up doing:

logger.info("Hello, World", null, this);
logger.info("Goodbye, World", null, this);

In the JSF snippet, it might be:

logger.info("Hello, World", null, getContext());

The context object would pass through the call stack and eventually get 
incorporated into the logging event.  You could then have a 
ServletContextAppender (or something like it do):

public void doAppend(LogRecord logEvent)
{
        ServletContext context = null;
        if(logEvent.getUserContext() instanceof ServletContext) {
            context = (ServletContext) logEvent.getUserContext();
        } else if(logEvent.getUserContext() instanceof Servlet) {
            context = ((Servlet) logEvent.getUserContext()).getContext();
...
        if(context != null) {
             ...
            context.log(...);
        }
}

We could likely have such a servlet appender in the default configuration since 
it would be innocuous unless a servlet context was passed.

It may not be the right answer for this problem, but I think it will be the 
right answer for a non-empty class of problems.



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-dev-h...@logging.apache.org

Reply via email to