| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.core.config.logging; |
| 17 | |
|
| 18 | |
import java.io.ByteArrayInputStream; |
| 19 | |
import java.io.File; |
| 20 | |
import java.io.FileInputStream; |
| 21 | |
import java.io.FileNotFoundException; |
| 22 | |
import java.io.IOException; |
| 23 | |
import java.util.Properties; |
| 24 | |
|
| 25 | |
import javax.xml.parsers.DocumentBuilder; |
| 26 | |
import javax.xml.parsers.DocumentBuilderFactory; |
| 27 | |
|
| 28 | |
import org.apache.log4j.LogManager; |
| 29 | |
import org.apache.log4j.Logger; |
| 30 | |
import org.apache.log4j.PropertyConfigurator; |
| 31 | |
import org.apache.log4j.helpers.FileWatchdog; |
| 32 | |
import org.apache.log4j.helpers.LogLog; |
| 33 | |
import org.apache.log4j.spi.LoggerRepository; |
| 34 | |
import org.apache.log4j.xml.DOMConfigurator; |
| 35 | |
import org.kuali.rice.core.config.Config; |
| 36 | |
import org.kuali.rice.core.config.ConfigContext; |
| 37 | |
import org.kuali.rice.core.lifecycle.BaseLifecycle; |
| 38 | |
import org.springframework.util.Log4jConfigurer; |
| 39 | |
import org.springframework.util.ResourceUtils; |
| 40 | |
import org.w3c.dom.Document; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 0 | public class Log4jLifeCycle extends BaseLifecycle { |
| 46 | |
|
| 47 | |
private static final String LOG4J_FILE_NOT_FOUND = "log4j settings file not found at location: "; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
private static final int MINUTE = 60 * 1000; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
private static final String AUTOMATIC_LOGGING_CONFIG_URL = "classpath:org/kuali/rice/core/logging/default-log4j.properties"; |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
private static final int DEFAULT_RELOAD_INTERVAL = 5 * MINUTE; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | 0 | private Logger log = Logger.getLogger(getClass()); |
| 68 | |
|
| 69 | |
public void start() throws Exception { |
| 70 | |
|
| 71 | 0 | Config config = ConfigContext.getRootConfig(); |
| 72 | |
|
| 73 | 0 | boolean log4jFileExists = checkPropertiesFileExists(config.getProperty(Config.LOG4J_SETTINGS_PATH)); |
| 74 | |
|
| 75 | |
|
| 76 | 0 | String log4jconfig = config.getProperty(Config.LOG4J_SETTINGS_XML); |
| 77 | 0 | if (log4jconfig != null) { |
| 78 | |
try { |
| 79 | 0 | DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 80 | 0 | Document doc = b.parse(new ByteArrayInputStream(log4jconfig.getBytes())); |
| 81 | 0 | DOMConfigurator.configure(doc.getDocumentElement()); |
| 82 | |
|
| 83 | 0 | log = Logger.getLogger(getClass()); |
| 84 | 0 | } catch (Exception e) { |
| 85 | 0 | log.error("Error parsing Log4J configuration settings: " + log4jconfig, e); |
| 86 | 0 | } |
| 87 | |
|
| 88 | 0 | } else if ((log4jconfig = config.getProperty(Config.LOG4J_SETTINGS_PROPS)) != null) { |
| 89 | 0 | Properties p = new Properties(config.getProperties()); |
| 90 | |
try { |
| 91 | 0 | p.load(new ByteArrayInputStream(log4jconfig.getBytes())); |
| 92 | 0 | PropertyConfigurator.configure(p); |
| 93 | 0 | log = Logger.getLogger(getClass()); |
| 94 | 0 | } catch (IOException ioe) { |
| 95 | 0 | log.error("Error loading Log4J configuration settings: " + log4jconfig, ioe); |
| 96 | 0 | } |
| 97 | |
|
| 98 | 0 | } else if (log4jFileExists) { |
| 99 | 0 | log.info("Configuring Log4J logging."); |
| 100 | 0 | log4jconfig = config.getProperty(Config.LOG4J_SETTINGS_PATH); |
| 101 | |
|
| 102 | 0 | int reloadInterval = DEFAULT_RELOAD_INTERVAL; |
| 103 | |
|
| 104 | 0 | String log4jReloadInterval = config.getProperty(Config.LOG4J_SETTINGS_RELOADINTERVAL_MINS); |
| 105 | 0 | if (log4jReloadInterval != null) { |
| 106 | |
try { |
| 107 | 0 | reloadInterval = Integer.parseInt(log4jReloadInterval) * MINUTE; |
| 108 | 0 | } catch (NumberFormatException nfe) { |
| 109 | 0 | log.warn("Invalid reload interval: " + log4jReloadInterval + ", using default: 5 minutes"); |
| 110 | 0 | } |
| 111 | |
} |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | 0 | if ("1.2.13".equals(getLog4jVersion())) { |
| 117 | 0 | log.info("Using custom Log4j 1.2.13 configurer to make workflow config properties accessible"); |
| 118 | |
|
| 119 | |
|
| 120 | 0 | WorkflowLog4j_1_2_13_Configurer.initLoggingWithProperties(config.getProperties(), log4jconfig, reloadInterval); |
| 121 | |
} else { |
| 122 | 0 | log.info("Using standard Log4jConfigurer"); |
| 123 | |
|
| 124 | 0 | PropertyConfigurator.configureAndWatch(log4jconfig, reloadInterval); |
| 125 | |
} |
| 126 | |
|
| 127 | 0 | log = Logger.getLogger(getClass()); |
| 128 | |
|
| 129 | 0 | } else { |
| 130 | |
|
| 131 | 0 | PropertyConfigurator.configureAndWatch(AUTOMATIC_LOGGING_CONFIG_URL, DEFAULT_RELOAD_INTERVAL); |
| 132 | 0 | log = Logger.getLogger(getClass()); |
| 133 | |
} |
| 134 | 0 | super.start(); |
| 135 | 0 | } |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
private boolean checkPropertiesFileExists(String log4jSettingsPath) { |
| 144 | |
boolean exists; |
| 145 | |
|
| 146 | |
try { |
| 147 | 0 | exists = ResourceUtils.getFile(log4jSettingsPath).exists(); |
| 148 | 0 | } catch (FileNotFoundException e) { |
| 149 | 0 | exists = false; |
| 150 | 0 | } |
| 151 | |
|
| 152 | 0 | if (!exists) { |
| 153 | 0 | System.out.println(LOG4J_FILE_NOT_FOUND + log4jSettingsPath); |
| 154 | |
} |
| 155 | |
|
| 156 | 0 | return exists; |
| 157 | |
} |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
private static String getLog4jVersion() { |
| 166 | 0 | Package p = Package.getPackage("org.apache.log4j"); |
| 167 | 0 | if (p == null) return null; |
| 168 | 0 | return p.getImplementationVersion(); |
| 169 | |
} |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | 0 | private static final class WorkflowLog4j_1_2_13_Configurer extends Log4jConfigurer { |
| 178 | |
public static void initLoggingWithProperties(Properties props, String location, long refreshInterval) throws FileNotFoundException { |
| 179 | 0 | File file = ResourceUtils.getFile(location); |
| 180 | 0 | if (!file.exists()) { |
| 181 | 0 | throw new FileNotFoundException("Log4J config file [" + location + "] not found"); |
| 182 | |
} |
| 183 | 0 | if (location.toLowerCase().endsWith(XML_FILE_EXTENSION)) { |
| 184 | 0 | DOMConfigurator.configureAndWatch(file.getAbsolutePath(), refreshInterval); |
| 185 | |
} else { |
| 186 | 0 | assert(props != null); |
| 187 | 0 | WorkflowLog4j_1_2_13_PropertyConfigurator.configureAndWatch(props, file.getAbsolutePath(), refreshInterval); |
| 188 | |
} |
| 189 | 0 | } |
| 190 | |
} |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | 0 | static final class WorkflowLog4j_1_2_13_PropertyConfigurator extends PropertyConfigurator { |
| 197 | |
static public void configureAndWatch(final Properties initialProperties, String configFilename, long delay) { |
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | 0 | FileWatchdog watchDog = new FileWatchdog(configFilename) { |
| 203 | |
public void doOnChange() { |
| 204 | 0 | new WorkflowLog4j_1_2_13_PropertyConfigurator().doConfigure(initialProperties, this.filename, LogManager.getLoggerRepository()); |
| 205 | 0 | } |
| 206 | |
}; |
| 207 | 0 | watchDog.setDelay(delay); |
| 208 | 0 | watchDog.start(); |
| 209 | 0 | } |
| 210 | |
|
| 211 | |
public void doConfigure(Properties initialProperties, String configFileName, LoggerRepository hierarchy) { |
| 212 | 0 | Properties props = new Properties(); |
| 213 | 0 | props.putAll(initialProperties); |
| 214 | |
|
| 215 | |
try { |
| 216 | 0 | FileInputStream istream = new FileInputStream(configFileName); |
| 217 | 0 | props.load(istream); |
| 218 | 0 | istream.close(); |
| 219 | |
} |
| 220 | 0 | catch (IOException e) { |
| 221 | 0 | LogLog.error("Could not read configuration file ["+configFileName+"].", e); |
| 222 | 0 | LogLog.error("Ignoring configuration file [" + configFileName+"]."); |
| 223 | 0 | return; |
| 224 | 0 | } |
| 225 | |
|
| 226 | 0 | doConfigure(props, hierarchy); |
| 227 | 0 | } |
| 228 | |
} |
| 229 | |
|
| 230 | |
public void stop() throws Exception { |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | 0 | super.stop(); |
| 238 | 0 | } |
| 239 | |
|
| 240 | |
} |