tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: Problem with using log4j Tomcat and web application level
Date Mon, 17 Sep 2012 19:33:23 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mark,

On 9/17/12 1:36 PM, Mark Eggers wrote:
> So really, for me, it boils down to this.
> 
> 1. Place log4j.xml in WEB-INF/classes 2. Write a servlet context
> listener a. contextInitialized - just write something to the log b.
> contextDestroyed - shut down logging if the app started it

Yup. Here's mine:

...
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;

import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
...
public class Log4jListener
    implements ServletContextListener
{
    private Logger logger;

    public void contextInitialized(ServletContextEvent e)
    {
        // Trigger loading of the log4j.properties file from the
classpath.
        new PropertyConfigurator();

        logger = Logger.getLogger(this.getClass());

        logger.info("Log4j initialized");
    }

    public void contextDestroyed(ServletContextEvent e)
    {
        if(LogManager.class.getClassLoader()
           .equals(this.getClass().getClassLoader()))
        {
            if(null != logger)
                logger.info("Log4j was loaded by application
classloader; shutting down.");

            LogManager.shutdown();
        }
        else
        {
            if(null != logger)
                logger.info("Log4j was loaded by some other
ClassLoader; not shutting down.");
        }
    }
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/

iEYEARECAAYFAlBXewMACgkQ9CaO5/Lv0PA0OwCeINt42NSM5okJtTKbNra46If/
AlUAoLfG43X8GgMV5fJeQsaV8G1sEB57
=Nuh2
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message