1packageorg.apache.jetspeed.services.logging;
23/*4 * Copyright 2001,2004 The Apache Software Foundation.5 * 6 * Licensed under the Apache License, Version 2.0 (the "License");7 * you may not use this file except in compliance with the License.8 * You may obtain a copy of the License at9 * 10 * http://www.apache.org/licenses/LICENSE-2.011 * 12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819// Java classes20import javax.servlet.ServletConfig;
21import javax.servlet.ServletContext;
2223// Jetspeed classes24import org.apache.jetspeed.services.resources.JetspeedResources;
2526// Log4J classes27import org.apache.log4j.LogManager;
28import org.apache.log4j.Logger;
29import org.apache.log4j.PropertyConfigurator;
30import org.apache.log4j.xml.DOMConfigurator;
3132// Turbine classes33import org.apache.turbine.Turbine;
34import org.apache.turbine.services.InitializationException;
35import org.apache.turbine.services.TurbineBaseService;
3637/***38 * The default implementation of the logging service in Jetspeed.39 *40 * This service initializes the underlying logging implementation, 41 * and acts as a factory for loggers.42 * The current implementation uses Log4J.43 *44 * @see org.apache.log4j.LogManager45 * @see org.apache.log4j.Logger46 * @author <a href="mailto:harald@ommang.com">Harald Ommang</a>47 */48publicclassJetspeedLogFactoryServiceextends TurbineBaseService
49 {
5051public String SERVICE_NAME = "JetspeedLogFactoryService";
52privatestaticfinal String CONFIG_LOG4J_PROPERTIES = "log4j.properties";
53privatestaticfinal String CONFIG_LOG4J_PROPERTIES_DEFAULT = "/WEB-INF/conf/log4j.properties";
54privatestaticfinal String CONFIG_LOG4J_AND_WATCH = "log4j.configureAndWatch";
55privatestaticfinalboolean CONFIG_LOG4J_AND_WATCH_DEFAULT = true;
56privatestaticfinal String CONFIG_LOG4J_WATCHINTERVAL = "log4j.watchInterval";
57privatestaticfinallong CONFIG_LOG4J_WATCHINTERVAL_DEFAULT = 60000L;
58private ServletContext context;
59/***60 * Flag to check for initilization. Needed to make time of init more robust.61 * Also, cannot access the init in parent class from static method62 */63privatestaticboolean initDone = false;
6465/***66 * Default constructor67 */68publicJetspeedLogFactoryService()
69 {
70 context = null;
71 }
7273/***74 * Initializes the service by getting the servlet configuration from Turbine75 *76 * @throws InitializationException Initialization failed77 */78publicvoid init() throws InitializationException
79 {
80 ServletConfig conf = Turbine.getTurbineServletConfig();
81if(conf != null)
82 {
83 init(conf);
84 }
85 }
8687/***88 * Initializes the service with the given configuration89 * Initializes the underlying logging implementation, Log4J90 *91 * @param config The ServletConfiguration from Turbine92 *93 * @throws InitializationException Initialization failed94 */95publicvoid init(ServletConfig config) throws InitializationException
96 {
97 context = config.getServletContext();
98 String log4jProperties = JetspeedResources.getString(CONFIG_LOG4J_PROPERTIES, CONFIG_LOG4J_PROPERTIES_DEFAULT);
99if(log4jProperties != null)
100 {
101try102 {
103 String fileName = Turbine.getRealPath(log4jProperties);
104boolean watch = JetspeedResources.getBoolean(CONFIG_LOG4J_AND_WATCH, CONFIG_LOG4J_AND_WATCH_DEFAULT);
105long watchInterval = JetspeedResources.getLong(CONFIG_LOG4J_WATCHINTERVAL, CONFIG_LOG4J_WATCHINTERVAL_DEFAULT);
106 System.setProperty("webappRoot", context.getRealPath("/"));
107108// Check to see if property or XML configuration is to be used.109if(fileName.endsWith(".properties"))
110 {
111if(watch)
112 {
113// Configure with a property file and watch for changes114 PropertyConfigurator.configureAndWatch(fileName, watchInterval);
115 }
116else117 {
118 PropertyConfigurator.configure(fileName);
119 }
120 }
121else122 {
123if(watch)
124 {
125// Configure with an XML file and watch for changes126 DOMConfigurator.configureAndWatch(fileName, watchInterval);
127 }
128else129 {
130 DOMConfigurator.configure(fileName);
131 }
132 }
133 }
134catch(Exception e)
135 {
136thrownew InitializationException("Failed to load " + log4jProperties + " - " + e.toString());
137 }
138 }
139 setInit(true);
140 initDone = true;
141 } // init142143/***144 * The actual Factory method that gets the appropriate logger from Log4j and145 * wraps it in a JetspeedLogger146 */147publicstaticJetspeedLogger getLogger(String loggerName)
148 {
149// This test needed to ensure correct init sequence between this and services that log.150if (!initDone)
151 {
152synchronized (JetspeedLogFactoryService.class)
153 {
154if (!initDone)
155 {
156try157 {
158newJetspeedLogFactoryService().init();
159 }
160catch(Exception e)
161 {
162 System.err.println("Init failed no logging available" + e.getMessage());
163 e.printStackTrace();
164 }
165 }
166 }
167 }
168 Logger newLog = LogManager.getLogger(loggerName);
169JetspeedLoggernewLogger = newJetspeedLogger(newLog);
170return newLogger;
171 }
172 } // class JetspeedLogFactoryService173174