1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.webapp.logging;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.Properties;
22
23 import javax.servlet.ServletContextEvent;
24 import javax.servlet.ServletContextListener;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.log4j.Hierarchy;
29 import org.apache.log4j.Level;
30 import org.apache.log4j.PropertyConfigurator;
31 import org.apache.log4j.spi.RootCategory;
32
33 public class Log4JConfigurator implements ServletContextListener
34 {
35 /*** The optional web.xml context parameter name for the Log4J Config File (location).
36 * The specified location is interpreted as begin (webapp) context relative.
37 */
38 public static final String LOG4J_CONFIG_FILE = "log4j.config.file";
39
40 /*** The default value for the Log4J Config File (location) */
41 public static final String LOG4J_CONFIG_FILE_DEFAULT = "/WEB-INF/log4j.properties";
42
43 /*** The optional web.xml context parameter name for the Log4J Config webApplicationRoot (property) */
44 public static final String LOG4J_CONFIG_WEB_APPLICATION_ROOT_KEY = "log4j.config.webApplicationRoot.key";
45
46 /*** The default value for the Log4J Config webApplicationRoot (property) */
47 public static final String LOG4J_CONFIG_WEB_APPLICATION_ROOT_KEY_DEFAULT = "webApplicationRoot";
48
49 private Hierarchy isolatedHierarchy;
50
51 private static Log log;
52
53 public void contextInitialized(ServletContextEvent event)
54 {
55 String log4JConfigFile = event.getServletContext().getInitParameter(LOG4J_CONFIG_FILE);
56 if ( log4JConfigFile == null || log4JConfigFile.length() == 0 )
57 {
58 log4JConfigFile = LOG4J_CONFIG_FILE_DEFAULT;
59 }
60 String rootPath = event.getServletContext().getRealPath("");
61 InputStream input = event.getServletContext().getResourceAsStream(log4JConfigFile);
62 if ( input != null )
63 {
64 try
65 {
66 Properties p = new Properties();
67 p.load(input);
68 String waRootKey = event.getServletContext().getInitParameter(LOG4J_CONFIG_WEB_APPLICATION_ROOT_KEY);
69 if ( waRootKey == null || waRootKey.length() == 0 )
70 {
71 waRootKey = LOG4J_CONFIG_WEB_APPLICATION_ROOT_KEY_DEFAULT;
72 }
73 p.setProperty(waRootKey,rootPath);
74 isolatedHierarchy = new Hierarchy(new RootCategory(Level.INFO));
75 new PropertyConfigurator().doConfigure(p,isolatedHierarchy);
76 IsolatedLog4JLogger.setHierarchy(isolatedHierarchy);
77 log = LogFactory.getLog(this.getClass());
78 log.info("IsolatedLog4JLogger configured");
79 }
80 catch (IOException ioe)
81 {
82 event.getServletContext().log("Failed to configure Log4J from "+event.getServletContext().getServletContextName(),ioe);
83 }
84 }
85 else
86 {
87 event.getServletContext().log(event.getServletContext().getServletContextName()+" Log4JConfigurator: "+rootPath+log4JConfigFile+" not found.");
88 }
89 }
90
91 public void contextDestroyed(ServletContextEvent event)
92 {
93 if ( log != null )
94 {
95 log.info("Shutting down IsolatedLog4JLogger");
96 log = null;
97 }
98
99
100 LogFactory.release(Thread.currentThread().getContextClassLoader());
101
102 if (isolatedHierarchy != null)
103 {
104 isolatedHierarchy.shutdown();
105 }
106 }
107 }