1 package org.apache.jetspeed.services.logging; 2 3 18 19 import javax.servlet.ServletConfig ; 21 import javax.servlet.ServletContext ; 22 23 import org.apache.jetspeed.services.resources.JetspeedResources; 25 26 import org.apache.log4j.LogManager; 28 import org.apache.log4j.Logger; 29 import org.apache.log4j.PropertyConfigurator; 30 import org.apache.log4j.xml.DOMConfigurator; 31 32 import org.apache.turbine.Turbine; 34 import org.apache.turbine.services.InitializationException; 35 import org.apache.turbine.services.TurbineBaseService; 36 37 48 public class JetspeedLogFactoryService extends TurbineBaseService 49 { 50 51 public String SERVICE_NAME = "JetspeedLogFactoryService"; 52 private static final String CONFIG_LOG4J_PROPERTIES = "log4j.properties"; 53 private static final String CONFIG_LOG4J_PROPERTIES_DEFAULT = "/WEB-INF/conf/log4j.properties"; 54 private static final String CONFIG_LOG4J_AND_WATCH = "log4j.configureAndWatch"; 55 private static final boolean CONFIG_LOG4J_AND_WATCH_DEFAULT = true; 56 private static final String CONFIG_LOG4J_WATCHINTERVAL = "log4j.watchInterval"; 57 private static final long CONFIG_LOG4J_WATCHINTERVAL_DEFAULT = 60000L; 58 private ServletContext context; 59 63 private static boolean initDone = false; 64 65 68 public JetspeedLogFactoryService() 69 { 70 context = null; 71 } 72 73 78 public void init() throws InitializationException 79 { 80 ServletConfig conf = Turbine.getTurbineServletConfig(); 81 if(conf != null) 82 { 83 init(conf); 84 } 85 } 86 87 95 public void init(ServletConfig config) throws InitializationException 96 { 97 context = config.getServletContext(); 98 String log4jProperties = JetspeedResources.getString(CONFIG_LOG4J_PROPERTIES, CONFIG_LOG4J_PROPERTIES_DEFAULT); 99 if(log4jProperties != null) 100 { 101 try 102 { 103 String fileName = Turbine.getRealPath(log4jProperties); 104 boolean watch = JetspeedResources.getBoolean(CONFIG_LOG4J_AND_WATCH, CONFIG_LOG4J_AND_WATCH_DEFAULT); 105 long watchInterval = JetspeedResources.getLong(CONFIG_LOG4J_WATCHINTERVAL, CONFIG_LOG4J_WATCHINTERVAL_DEFAULT); 106 System.setProperty("webappRoot", context.getRealPath("/")); 107 108 if(fileName.endsWith(".properties")) 110 { 111 if(watch) 112 { 113 PropertyConfigurator.configureAndWatch(fileName, watchInterval); 115 } 116 else 117 { 118 PropertyConfigurator.configure(fileName); 119 } 120 } 121 else 122 { 123 if(watch) 124 { 125 DOMConfigurator.configureAndWatch(fileName, watchInterval); 127 } 128 else 129 { 130 DOMConfigurator.configure(fileName); 131 } 132 } 133 } 134 catch(Exception e) 135 { 136 throw new InitializationException("Failed to load " + log4jProperties + " - " + e.toString()); 137 } 138 } 139 setInit(true); 140 initDone = true; 141 } 143 147 public static JetspeedLogger getLogger(String loggerName) 148 { 149 if (!initDone) 151 { 152 synchronized (JetspeedLogFactoryService.class) 153 { 154 if (!initDone) 155 { 156 try 157 { 158 new JetspeedLogFactoryService().init(); 159 } 160 catch(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); 169 JetspeedLogger newLogger = new JetspeedLogger(newLog); 170 return newLogger; 171 } 172 } 174 175 | Popular Tags |