Ralph Goers | 1 Jun 2010 01:56
Favicon

Re: Improving log4j so it can easily be used with servlet logging


On May 31, 2010, at 3:53 PM, Curt Arnold wrote:

> 
> 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);

I would object to this - see my other post.  I could tolerate this if it was 

Logger.into(Object message, Throwable thrown, Context context);

But since the Context is likely to have the same life expectancy as the LoggerContext it makes more sense to
just tie those together. 

public class LoggerContextListener implements ServletContextListener {
  public void contextInitialized(ServletContextEvent event) {
  	LogManager.setContext(new ServletLoggerContext(event.getServletContext()));
  }

}

public class ServletLoggerContext extends LoggerContext {

	private ServletContext context;	
	
	public ServletLoggerContext(ServletContext context) {
		super();
		this.context = context;
        }

	public Object getExternalContext() {
		return this.context;
        }
}

Ralph

Gmane