| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.core.impl.config.module; |
| 18 | |
|
| 19 | |
import org.apache.commons.lang.StringUtils; |
| 20 | |
import org.apache.log4j.Logger; |
| 21 | |
import org.apache.log4j.PropertyConfigurator; |
| 22 | |
import org.apache.log4j.xml.DOMConfigurator; |
| 23 | |
import org.kuali.rice.core.api.config.property.Config; |
| 24 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
| 25 | |
import org.kuali.rice.core.api.lifecycle.BaseLifecycle; |
| 26 | |
import org.springframework.util.ResourceUtils; |
| 27 | |
import org.w3c.dom.Document; |
| 28 | |
|
| 29 | |
import javax.xml.parsers.DocumentBuilder; |
| 30 | |
import javax.xml.parsers.DocumentBuilderFactory; |
| 31 | |
import java.io.ByteArrayInputStream; |
| 32 | |
import java.io.FileNotFoundException; |
| 33 | |
import java.io.IOException; |
| 34 | |
import java.util.Properties; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0 | class Log4jLifeCycle extends BaseLifecycle { |
| 40 | |
|
| 41 | |
private static final String LOG4J_FILE_NOT_FOUND = "log4j settings file not found at location: "; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
private static final int MINUTE = 60 * 1000; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
private static final String AUTOMATIC_LOGGING_CONFIG_URL = "classpath:org/kuali/rice/core/logging/default-log4j.properties"; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
private static final int DEFAULT_RELOAD_INTERVAL = 5 * MINUTE; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | 0 | private Logger log = Logger.getLogger(getClass()); |
| 62 | |
|
| 63 | |
@Override |
| 64 | |
public void start() throws Exception { |
| 65 | |
|
| 66 | 0 | Config config = ConfigContext.getCurrentContextConfig(); |
| 67 | |
|
| 68 | 0 | boolean log4jFileExists = checkPropertiesFileExists(config.getProperty(Config.LOG4J_SETTINGS_PATH)); |
| 69 | |
|
| 70 | |
|
| 71 | 0 | String log4jconfig = config.getProperty(Config.LOG4J_SETTINGS_XML); |
| 72 | 0 | if (log4jconfig != null) { |
| 73 | |
try { |
| 74 | 0 | DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 75 | 0 | Document doc = b.parse(new ByteArrayInputStream(log4jconfig.getBytes())); |
| 76 | 0 | DOMConfigurator.configure(doc.getDocumentElement()); |
| 77 | |
|
| 78 | 0 | log = Logger.getLogger(getClass()); |
| 79 | 0 | } catch (Exception e) { |
| 80 | 0 | log.error("Error parsing Log4J configuration settings: " + log4jconfig, e); |
| 81 | 0 | } |
| 82 | |
|
| 83 | 0 | } else if ((log4jconfig = config.getProperty(Config.LOG4J_SETTINGS_PROPS)) != null) { |
| 84 | 0 | Properties p = new Properties(config.getProperties()); |
| 85 | |
try { |
| 86 | 0 | p.load(new ByteArrayInputStream(log4jconfig.getBytes())); |
| 87 | 0 | PropertyConfigurator.configure(p); |
| 88 | 0 | log = Logger.getLogger(getClass()); |
| 89 | 0 | } catch (IOException ioe) { |
| 90 | 0 | log.error("Error loading Log4J configuration settings: " + log4jconfig, ioe); |
| 91 | 0 | } |
| 92 | |
|
| 93 | 0 | } else if (log4jFileExists) { |
| 94 | 0 | log.info("Configuring Log4J logging."); |
| 95 | 0 | log4jconfig = config.getProperty(Config.LOG4J_SETTINGS_PATH); |
| 96 | |
|
| 97 | 0 | int reloadInterval = DEFAULT_RELOAD_INTERVAL; |
| 98 | |
|
| 99 | 0 | String log4jReloadInterval = config.getProperty(Config.LOG4J_SETTINGS_RELOADINTERVAL_MINS); |
| 100 | 0 | if (log4jReloadInterval != null) { |
| 101 | |
try { |
| 102 | 0 | reloadInterval = Integer.parseInt(log4jReloadInterval) * MINUTE; |
| 103 | 0 | } catch (NumberFormatException nfe) { |
| 104 | 0 | log.warn("Invalid reload interval: " + log4jReloadInterval + ", using default: " + DEFAULT_RELOAD_INTERVAL + " milliseconds"); |
| 105 | 0 | } |
| 106 | |
} |
| 107 | 0 | PropertyConfigurator.configureAndWatch(log4jconfig, reloadInterval); |
| 108 | 0 | log = Logger.getLogger(getClass()); |
| 109 | 0 | } else { |
| 110 | |
|
| 111 | 0 | PropertyConfigurator.configureAndWatch(AUTOMATIC_LOGGING_CONFIG_URL, DEFAULT_RELOAD_INTERVAL); |
| 112 | 0 | log = Logger.getLogger(getClass()); |
| 113 | |
} |
| 114 | 0 | super.start(); |
| 115 | 0 | } |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
private boolean checkPropertiesFileExists(String log4jSettingsPath) { |
| 124 | 0 | if (StringUtils.isBlank(log4jSettingsPath)) { |
| 125 | 0 | return false; |
| 126 | |
} |
| 127 | |
|
| 128 | |
boolean exists; |
| 129 | |
|
| 130 | |
try { |
| 131 | 0 | exists = ResourceUtils.getFile(log4jSettingsPath).exists(); |
| 132 | 0 | } catch (FileNotFoundException e) { |
| 133 | 0 | exists = false; |
| 134 | 0 | } |
| 135 | |
|
| 136 | 0 | if (!exists) { |
| 137 | 0 | System.out.println(LOG4J_FILE_NOT_FOUND + log4jSettingsPath); |
| 138 | |
} |
| 139 | |
|
| 140 | 0 | return exists; |
| 141 | |
} |
| 142 | |
|
| 143 | |
@Override |
| 144 | |
public void stop() throws Exception { |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | 0 | super.stop(); |
| 152 | 0 | } |
| 153 | |
|
| 154 | |
} |