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