Here is the code for ServletLogAppender.java. Basically, all you need to do to use it is put the right "appender" section in your log4j properties:
log4j.[[category]] = ... , servletAppender, ... log4j.servletAppender=com.vecna.servlet.ServletLogAppender and be sure to call ServletLogAppender.setServletContext(...) at least once prior to use. The easiest way to do this is to initialize Log4j from within a servlet context loader or a load-on-startup servlet, when you have the servlet context object available. The comments reference Log4jConfigurator, which is servlet context listener that boils down to this: public void contextInitialized (ServletContextEvent sce) { Properties props = [[ grab properties from somewhere ]]; PropertyConfigurator.configure(props); ServletLogAppender.setServletContext(sce.getServletContext()); } Hope you find it useful! -- Bill -------------------------------------- package com.vecna.servlet; import org.apache.log4j.AppenderSkeleton; import org.apache.log4j.Priority; import org.apache.log4j.spi.ThrowableInformation; import org.apache.log4j.spi.LoggingEvent; import javax.servlet.ServletContext; /** * ©1998-2001 Vecna Technologies, Inc. All Rights Reserved. * <br>$Source: /home/cvsroot/webtools/src/servlet/ServletLogAppender.java,v $ * * Wrapper class that adapts a servlet context's logger to work within * the log4j framework. * <p> * * This appender cannot be used until the setServletContext method is * called. If you initialize Log4j using the Log4jConfigurator class, * this will be done for you. The servletContext property is static; * because each webapp gets its own classloader there is a one-to-one * mapping between servlet contexts and instances of * ServletLogAppender.class. * * @author Bill Schneider ([EMAIL PROTECTED]) */ public class ServletLogAppender extends AppenderSkeleton { /** * Servlet Context */ private static ServletContext s_sctx; /** * Sets the servlet context for all ServletLogAppenders within * the current class loader (one per webapp). * Logging events will be appended to the servlet context's logger. * @param sctx the servlet context */ public static void setServletContext(ServletContext sctx) { s_sctx = sctx; } /** * Closes this appender. This is a no-op. */ public void close() { // do nothing } /** * Always returns false because the formatting of the log statement * is handled by the servlet context, not by log4j * @return always false */ public boolean requiresLayout() { return false; } /** * Logs <code>event</code> into the servlet context by calling * context.log. */ protected void append(LoggingEvent event) { if (s_sctx == null) { throw new IllegalStateException ("ServletLogAppender.append called before setServletContext"); } String errorDescription = event.getRenderedMessage(); ThrowableInformation ti = event.getThrowableInformation(); if (ti != null) { Throwable t = ti.getThrowable(); s_sctx.log(errorDescription, t); } else { s_sctx.log(errorDescription); } } } -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>