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</listener-class> </listener> <!-- ... --> </web-app>
log4j.properties:
log4j.rootLogger=DEBUG, catalina log4j.appender.catalina=de.fzi.dbs.commons.log4j.ServletContextLogAppender 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]