Howdy, Thanks for the contribution ;) I've actually done similar stuff, then decided to go away from it, reserving the context logs for server-specific output, but I can see the opposite use case as well.
We'd have to be careful to make it very clear that this requires a Servlet Specification v2.3 or later container. There are still a ton of 2.2 containers out there... Which would choke with a <listener> element in web.xml... I wouldn't mind adding some documentation to it. As an implementation note, what happens if there are two servlet contexts with the same name? This is completely legal, as the servlet context's name is optional (null is legal) and not required to be unique. You might want to use another ID as the key to the context map in your listener, e.g. the ServletContext object itself, its hashcode, or what it returns for getResource("/") (the latter is the closest you'll get to the context path, which is unique for a container instance. Yoav Shapira Millennium ChemInformatics >-----Original Message----- >From: Jacob Kjome [mailto:[EMAIL PROTECTED] >Sent: Monday, August 25, 2003 12:30 AM >To: Log4J Developers List >Subject: Re: Servlet context log appender. > > >Great stuff Aleksei! Very nice idea. Not sure why I didn't think of doing >this before? Seems so natural in a servlet environment. I tested it out >and it works perfectly :-) > >I'd like to see this go into the log4j-sandbox. In my local repository, I >have your classes set up like the following... > >org.apache.log4j.appender.ServletContextLogAppender >org.apache.log4j.servlet.ServletContextLogAppenderListener > >As you can see, I changed the package and the names of your classes to be a >better fit for the log4j-sandbox. Let me know if you think they should be >different. > >If no one else has objects to these classes going into the log4j-sandbox, >I'll check them in as-is. Let me know if any changes to packages, class >names, or design are desired. > >Oh, and should Aleksei get committer access to the log4j-sandbox so he can >modify/update his contribution? There isn't any explanation of how to use >this stuff in the Javadoc. The extra info he provided at the top of his >email should probably be added to the Javadoc of these classes. Giving >Aleksei committer access would allow him to add the appropriate >documentation after this stuff has been committed without having to >resubmit modifications. > >Jake > > >At 12:01 PM 8/24/2003 +0200, you wrote: >>Hi everyone. >> >> >>I'm using log4j for a long time already and since I'm mainly into web >>projects, I always wanted a tighter integration with "standard" web >>applications logging. basically, I wanted a log4j appender, which would >>output to the standard servlet context log (somehow). >> >>Last week I finally took an hour to implements this. I'd like you to >>review my solution and take it as contribution, if suitable. >> >>The idea is to use ServletContextListener to receive ServletContext of the >>web application and provide this context to the appender. >> >>Here's how it works: >> >>Configuration example: >> >>web.xml: >> >><?xml version="1.0" encoding="ISO-8859-1"?> >><!DOCTYPE web-app >> PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" >> "http://java.sun.com/dtd/web-app_2_3.dtd"> >><web-app> >> <!-- ... --> >> <display-name>My Application</display-name> >> <!-- ... --> >> <listener> >> >><listener-class>de.fzi.dbs.commons.log4j.Log4jContextListener</listene r- >class> >> </listener> >> <!-- ... --> >></web-app> >> >>log4j.properties: >> >>log4j.rootLogger=DEBUG, catalina >>log4j.appender.catalina=de.fzi.dbs.commons.log4j.ServletContextLogAppe nder >>log4j.appender.catalina.servletContextName=My Application >>log4j.appender.catalina.layout=org.apache.log4j.PatternLayout >>log4j.appender.catalina.layout.ConversionPattern=[%-5p][%4t] %m >> >>Classes (also in the attachment): >> >>/* >> * >> >======================================================================= ==== >= >> * The Apache Software License, Version 1.1 >> * >> >======================================================================= ==== >= >> * >> * Copyright (C) 1999 The Apache Software Foundation. All rights >reserved. >> * >> * Redistribution and use in source and binary forms, with or without >> modifica- >> * tion, are permitted provided that the following conditions are met: >> * >> * 1. Redistributions of source code must retain the above >> copyright notice, >> * this list of conditions and the following disclaimer. >> * >> * 2. Redistributions in binary form must reproduce the above copyright >> notice, >> * this list of conditions and the following disclaimer in the >> documentation >> * and/or other materials provided with the distribution. >> * >> * 3. The end-user documentation included with the redistribution, if >> any, must >> * include the following acknowledgment: "This product includes >> software >> * developed by the Apache Software Foundation >> (http://www.apache.org/)." >> * Alternately, this acknowledgment may appear in the software >> itself, if >> * and wherever such third-party acknowledgments normally appear. >> * >> * 4. The names "log4j" and "Apache Software Foundation" must not be >> used to >> * endorse or promote products derived from this software >> without prior >> * written permission. For written permission, please contact >> * [EMAIL PROTECTED] >> * >> * 5. Products derived from this software may not be called "Apache", >> nor may >> * "Apache" appear in their name, without prior written >> permission of the >> * Apache Software Foundation. >> * >> * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED >> WARRANTIES, >> * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF >> MERCHANTABILITY AND >> * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT >> SHALL THE >> * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY >> DIRECT, >> * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES >> (INCLU- >> * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR >> SERVICES; LOSS >> * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED >> AND ON >> * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT >> LIABILITY, OR TORT >> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF >> THE USE OF >> * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. >> * >> * This software consists of voluntary contributions made by many >> individuals >> * on behalf of the Apache Software Foundation. For more information >> on the >> * Apache Software Foundation, please see <http://www.apache.org/>. >> * >> */ >>package de.fzi.dbs.commons.log4j; >> >>import javax.servlet.ServletContextListener; >>import javax.servlet.ServletContextEvent; >>import javax.servlet.ServletContext; >> >>/** >> * This class maintains list of contexts in ServletContextLogAppender. >> * @author Aleksei Valikov >> */ >>public class Log4jContextListener implements ServletContextListener >>{ >> /** >> * Receives a notification of the context initialization event. >> * @param event context event. >> */ >> public void contextInitialized(final ServletContextEvent event) >> { >> // Add context to map of servlet contexts >> final ServletContext servletContext = event.getServletContext(); >> ServletContextLogAppender.servletContexts.put( >> servletContext.getServletContextName(), >> servletContext); >> } >> >> /** >> * Receives a notification of the context destruction event. >> * @param event context event. >> */ >> public void contextDestroyed(final ServletContextEvent event) >> { >> // Removes context from the map of servlet contexts >> final ServletContext servletContext = event.getServletContext(); >> ServletContextLogAppender.servletContexts.remove( >> servletContext.getServletContextName()); >> } >>} >> >>/* >> * >> >======================================================================= ==== >= >> * The Apache Software License, Version 1.1 >> * >> >======================================================================= ==== >= >> * >> * Copyright (C) 1999 The Apache Software Foundation. All rights >reserved. >> * >> * Redistribution and use in source and binary forms, with or without >> modifica- >> * tion, are permitted provided that the following conditions are met: >> * >> * 1. Redistributions of source code must retain the above >> copyright notice, >> * this list of conditions and the following disclaimer. >> * >> * 2. Redistributions in binary form must reproduce the above copyright >> notice, >> * this list of conditions and the following disclaimer in the >> documentation >> * and/or other materials provided with the distribution. >> * >> * 3. The end-user documentation included with the redistribution, if >> any, must >> * include the following acknowledgment: "This product includes >> software >> * developed by the Apache Software Foundation >> (http://www.apache.org/)." >> * Alternately, this acknowledgment may appear in the software >> itself, if >> * and wherever such third-party acknowledgments normally appear. >> * >> * 4. The names "log4j" and "Apache Software Foundation" must not be >> used to >> * endorse or promote products derived from this software >> without prior >> * written permission. For written permission, please contact >> * [EMAIL PROTECTED] >> * >> * 5. Products derived from this software may not be called "Apache", >> nor may >> * "Apache" appear in their name, without prior written >> permission of the >> * Apache Software Foundation. >> * >> * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED >> WARRANTIES, >> * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF >> MERCHANTABILITY AND >> * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT >> SHALL THE >> * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY >> DIRECT, >> * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES >> (INCLU- >> * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR >> SERVICES; LOSS >> * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED >> AND ON >> * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT >> LIABILITY, OR TORT >> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF >> THE USE OF >> * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. >> * >> * This software consists of voluntary contributions made by many >> individuals >> * on behalf of the Apache Software Foundation. For more information >> on the >> * Apache Software Foundation, please see <http://www.apache.org/>. >> * >> */ >>package de.fzi.dbs.commons.log4j; >> >>import org.apache.log4j.AppenderSkeleton; >>import org.apache.log4j.spi.LoggingEvent; >> >>import javax.servlet.ServletContext; >>import java.util.Map; >>import java.util.HashMap; >>import java.util.Collections; >> >>/** >> * Appends log messages to the servlet context log. >> * @author Aleksei Valikov >> */ >>public class ServletContextLogAppender extends AppenderSkeleton >>{ >> /** Map of servlet contexts. */ >> public static Map servletContexts = >> Collections.synchronizedMap(new HashMap()); >> >> /** Name of the servlet context of this appender instance. */ >> protected String servletContextName; >> >> /** Servlet context of this appender. */ >> protected ServletContext servletContext; >> >> /** >> * Returns servlet context name of this appender. >> * @return Servlet context name of this appender. >> */ >> public String getServletContextName() >> { >> return servletContextName; >> } >> >> /** >> * Sets servlet context name of this appender. >> * @param servletContextName name of the servlet context of this >appender. >> */ >> public void setServletContextName(final String servletContextName) >> { >> this.servletContextName = servletContextName; >> } >> >> /** >> * Activates configured options. >> */ >> public void activateOptions() >> { >> servletContext = (ServletContext) >> servletContexts.get(servletContextName); >> if (servletContext == null) >> errorHandler.error("Servlet context [" + >> servletContextName + >> "] could not be found."); >> } >> >> /** >> * Appends a logging event through the servlet context logger. >> * @param event logging event to append; >> */ >> protected void append(final LoggingEvent event) >> { >> // If servlet context is undefined >> // Try loading servlet context >> if (servletContext == null) >> servletContext = (ServletContext) >> servletContexts.get(servletContextName); >> // If servlet context is not found, signal an error >> if (servletContext == null) >> { >> errorHandler.error("Servlet context [" + >> servletContextName + >> "] could not be found."); >> return; >> } >> // Output log message >> servletContext.log(layout.format(event)); >> return; >> } >> >> /** >> * Flags that layout is required. >> * @return Returns <code>true</code>. >> */ >> public boolean requiresLayout() >> { >> return true; >> } >> >> /** >> * Should close the appender - does nothing. >> */ >> public void close() >> { >> } >>} >> >>Please review let me know what you think. >>I feel it to be a useful development. Me personally, I've completely >>switched web app logging to this solution. >> >>Bye. >>/lexi >> >> >>/* >> * >> >======================================================================= ==== >= >> * The Apache Software License, Version 1.1 >> * >> >======================================================================= ==== >= >> * >> * Copyright (C) 1999 The Apache Software Foundation. All rights >reserved. >> * >> * Redistribution and use in source and binary forms, with or without >> modifica- >> * tion, are permitted provided that the following conditions are met: >> * >> * 1. Redistributions of source code must retain the above >> copyright notice, >> * this list of conditions and the following disclaimer. >> * >> * 2. Redistributions in binary form must reproduce the above copyright >> notice, >> * this list of conditions and the following disclaimer in the >> documentation >> * and/or other materials provided with the distribution. >> * >> * 3. The end-user documentation included with the redistribution, if >> any, must >> * include the following acknowledgment: "This product >> includes software >> * developed by the Apache Software >> Foundation (http://www.apache.org/)." >> * Alternately, this acknowledgment may appear in the software >> itself, if >> * and wherever such third-party acknowledgments normally appear. >> * >> * 4. The names "log4j" and "Apache Software Foundation" must not be >> used to >> * endorse or promote products derived from this software >> without prior >> * written permission. For written permission, please contact >> * [EMAIL PROTECTED] >> * >> * 5. Products derived from this software may not be called "Apache", >> nor may >> * "Apache" appear in their name, without prior written >> permission of the >> * Apache Software Foundation. >> * >> * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED >> WARRANTIES, >> * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF >> MERCHANTABILITY AND >> * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT >> SHALL THE >> * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY >> DIRECT, >> * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES >> (INCLU- >> * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR >> SERVICES; LOSS >> * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED >> AND ON >> * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT >> LIABILITY, OR TORT >> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF >> THE USE OF >> * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. >> * >> * This software consists of voluntary contributions made by many >> individuals >> * on behalf of the Apache Software Foundation. For more information >> on the >> * Apache Software Foundation, please see <http://www.apache.org/>. >> * >> */ >>package de.fzi.dbs.commons.log4j; >> >>import org.apache.log4j.AppenderSkeleton; >>import org.apache.log4j.spi.LoggingEvent; >> >>import javax.servlet.ServletContext; >>import java.util.Map; >>import java.util.HashMap; >>import java.util.Collections; >> >>/** >> * Appends log messages to the servlet context log. >> * @author Aleksei Valikov >> */ >>public class ServletContextLogAppender extends AppenderSkeleton >>{ >> /** Map of servlet contexts. */ >> public static Map servletContexts = >> Collections.synchronizedMap(new HashMap()); >> >> /** Name of the servlet context of this appender instance. */ >> protected String servletContextName; >> >> /** Servlet context of this appender. */ >> protected ServletContext servletContext; >> >> /** >> * Returns servlet context name of this appender. >> * @return Servlet context name of this appender. >> */ >> public String getServletContextName() >> { >> return servletContextName; >> } >> >> /** >> * Sets servlet context name of this appender. >> * @param servletContextName name of the servlet context of this >appender. >> */ >> public void setServletContextName(final String servletContextName) >> { >> this.servletContextName = servletContextName; >> } >> >> /** >> * Activates configured options. >> */ >> public void activateOptions() >> { >> servletContext = (ServletContext) >> servletContexts.get(servletContextName); >> if (servletContext == null) >> errorHandler.error("Servlet context [" + >> servletContextName + >> "] could not be found."); >> } >> >> /** >> * Appends a logging event through the servlet context logger. >> * @param event logging event to append; >> */ >> protected void append(final LoggingEvent event) >> { >> // If servlet context is undefined >> // Try loading servlet context >> if (servletContext == null) >> servletContext = (ServletContext) >> servletContexts.get(servletContextName); >> // If servlet context is not found, signal an error >> if (servletContext == null) >> { >> errorHandler.error("Servlet context [" + >> servletContextName + >> "] could not be found."); >> return; >> } >> // Output log message >> servletContext.log(layout.format(event)); >> return; >> } >> >> /** >> * Flags that layout is required. >> * @return Returns <code>true</code>. >> */ >> public boolean requiresLayout() >> { >> return true; >> } >> >> /** >> * Should close the appender - does nothing. >> */ >> public void close() >> { >> } >>}/* >> * >> >======================================================================= ==== >= >> * The Apache Software License, Version 1.1 >> * >> >======================================================================= ==== >= >> * >> * Copyright (C) 1999 The Apache Software Foundation. All rights >reserved. >> * >> * Redistribution and use in source and binary forms, with or without >> modifica- >> * tion, are permitted provided that the following conditions are met: >> * >> * 1. Redistributions of source code must retain the above >> copyright notice, >> * this list of conditions and the following disclaimer. >> * >> * 2. Redistributions in binary form must reproduce the above copyright >> notice, >> * this list of conditions and the following disclaimer in the >> documentation >> * and/or other materials provided with the distribution. >> * >> * 3. The end-user documentation included with the redistribution, if >> any, must >> * include the following acknowledgment: "This product >> includes software >> * developed by the Apache Software >> Foundation (http://www.apache.org/)." >> * Alternately, this acknowledgment may appear in the software >> itself, if >> * and wherever such third-party acknowledgments normally appear. >> * >> * 4. The names "log4j" and "Apache Software Foundation" must not be >> used to >> * endorse or promote products derived from this software >> without prior >> * written permission. For written permission, please contact >> * [EMAIL PROTECTED] >> * >> * 5. Products derived from this software may not be called "Apache", >> nor may >> * "Apache" appear in their name, without prior written >> permission of the >> * Apache Software Foundation. >> * >> * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED >> WARRANTIES, >> * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF >> MERCHANTABILITY AND >> * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT >> SHALL THE >> * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY >> DIRECT, >> * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES >> (INCLU- >> * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR >> SERVICES; LOSS >> * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED >> AND ON >> * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT >> LIABILITY, OR TORT >> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF >> THE USE OF >> * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. >> * >> * This software consists of voluntary contributions made by many >> individuals >> * on behalf of the Apache Software Foundation. For more information >> on the >> * Apache Software Foundation, please see <http://www.apache.org/>. >> * >> */ >>package de.fzi.dbs.commons.log4j; >> >>import javax.servlet.ServletContextListener; >>import javax.servlet.ServletContextEvent; >>import javax.servlet.ServletContext; >> >>/** >> * This class maintains list of contexts in ServletContextLogAppender. >> * @author Aleksei Valikov >> */ >>public class Log4jContextListener implements ServletContextListener >>{ >> /** >> * Receives a notification of the context initialization event. >> * @param event context event. >> */ >> public void contextInitialized(final ServletContextEvent event) >> { >> // Add context to map of servlet contexts >> final ServletContext servletContext = event.getServletContext(); >> ServletContextLogAppender.servletContexts.put( >> servletContext.getServletContextName(), >> servletContext); >> } >> >> /** >> * Receives a notification of the context destruction event. >> * @param event context event. >> */ >> public void contextDestroyed(final ServletContextEvent event) >> { >> // Removes context from the map of servlet contexts >> final ServletContext servletContext = event.getServletContext(); >> ServletContextLogAppender.servletContexts.remove( >> servletContext.getServletContextName()); >> } >>} >>--------------------------------------------------------------------- >>To unsubscribe, e-mail: [EMAIL PROTECTED] >>For additional commands, e-mail: [EMAIL PROTECTED] > > >--------------------------------------------------------------------- >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED] This e-mail, including any attachments, is a confidential business communication, and may contain information that is confidential, proprietary and/or privileged. This e-mail is intended only for the individual(s) to whom it is addressed, and may not be saved, copied, printed, disclosed or used by anyone else. If you are not the(an) intended recipient, please immediately delete this e-mail from your computer system and notify the sender. Thank you. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]