001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.webapp.logging;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.Properties;
022:
023: import javax.servlet.ServletContextEvent;
024: import javax.servlet.ServletContextListener;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.log4j.Hierarchy;
029: import org.apache.log4j.Level;
030: import org.apache.log4j.PropertyConfigurator;
031: import org.apache.log4j.spi.RootCategory;
032:
033: public class Log4JConfigurator implements ServletContextListener {
034: /** The optional web.xml context parameter name for the Log4J Config File (location).
035: * The specified location is interpreted as begin (webapp) context relative.
036: */
037: public static final String LOG4J_CONFIG_FILE = "log4j.config.file";
038:
039: /** The default value for the Log4J Config File (location) */
040: public static final String LOG4J_CONFIG_FILE_DEFAULT = "/WEB-INF/log4j.properties";
041:
042: /** The optional web.xml context parameter name for the Log4J Config webApplicationRoot (property) */
043: public static final String LOG4J_CONFIG_WEB_APPLICATION_ROOT_KEY = "log4j.config.webApplicationRoot.key";
044:
045: /** The default value for the Log4J Config webApplicationRoot (property) */
046: public static final String LOG4J_CONFIG_WEB_APPLICATION_ROOT_KEY_DEFAULT = "webApplicationRoot";
047:
048: private Hierarchy isolatedHierarchy;
049:
050: private static Log log;
051:
052: public void contextInitialized(ServletContextEvent event) {
053: String log4JConfigFile = event.getServletContext()
054: .getInitParameter(LOG4J_CONFIG_FILE);
055: if (log4JConfigFile == null || log4JConfigFile.length() == 0) {
056: log4JConfigFile = LOG4J_CONFIG_FILE_DEFAULT;
057: }
058: String rootPath = event.getServletContext().getRealPath("");
059: InputStream input = event.getServletContext()
060: .getResourceAsStream(log4JConfigFile);
061: if (input != null) {
062: try {
063: Properties p = new Properties();
064: p.load(input);
065: String waRootKey = event.getServletContext()
066: .getInitParameter(
067: LOG4J_CONFIG_WEB_APPLICATION_ROOT_KEY);
068: if (waRootKey == null || waRootKey.length() == 0) {
069: waRootKey = LOG4J_CONFIG_WEB_APPLICATION_ROOT_KEY_DEFAULT;
070: }
071: p.setProperty(waRootKey, rootPath);
072: isolatedHierarchy = new Hierarchy(new RootCategory(
073: Level.INFO));
074: new PropertyConfigurator().doConfigure(p,
075: isolatedHierarchy);
076: IsolatedLog4JLogger.setHierarchy(isolatedHierarchy);
077: log = LogFactory.getLog(this .getClass());
078: log.info("IsolatedLog4JLogger configured");
079: } catch (IOException ioe) {
080: event.getServletContext().log(
081: "Failed to configure Log4J from "
082: + event.getServletContext()
083: .getServletContextName(), ioe);
084: }
085: } else {
086: event.getServletContext().log(
087: event.getServletContext().getServletContextName()
088: + " Log4JConfigurator: " + rootPath
089: + log4JConfigFile + " not found.");
090: }
091: }
092:
093: public void contextDestroyed(ServletContextEvent event) {
094: if (log != null) {
095: log.info("Shutting down IsolatedLog4JLogger");
096: log = null;
097: }
098: // flush Logger cache which might be kept in a shared context if
099: // commons-logging isn't loaded through this ContextClassLoader
100: LogFactory.release(Thread.currentThread()
101: .getContextClassLoader());
102: // shutdown Log4J hierarchy (log4J keeps log files locked on Windows otherwise)
103: if (isolatedHierarchy != null) {
104: isolatedHierarchy.shutdown();
105: }
106: }
107: }
|