1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.context;
17
18 import java.io.File;
19 import java.net.URL;
20 import java.net.URLClassLoader;
21 import java.text.NumberFormat;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.log4j.PropertyConfigurator;
28 import org.kuali.ole.sys.OLEConstants;
29 import org.kuali.rice.core.api.config.property.ConfigContext;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39
40
41
42 public class Log4jConfigurer {
43 private static final Logger logger = LoggerFactory.getLogger(Log4jConfigurer.class);
44 private static final double MILLISECONDS_CONVERSION_MULTIPLIER = 60 * 1000;
45 private static final NumberFormat NF = getNumberFormatter();
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public static final void configureLogging(boolean doStartupStatsLogging) {
60 boolean override = isOverride();
61 logger.info(OLEConstants.LOG4J_OVERRIDE_KEY + "=" + override);
62 if (!override) {
63 return;
64 }
65
66 File customConfigFile = getCustomConfigFile();
67 long reloadMillis = getReloadMillis();
68 double minutes = reloadMillis / MILLISECONDS_CONVERSION_MULTIPLIER;
69 logger.info("Reconfiguring log4j using [" + customConfigFile + "] Reload interval is " + NF.format(minutes) + " minutes");
70 PropertyConfigurator.configureAndWatch(customConfigFile.getAbsolutePath(), reloadMillis);
71 debugClasspath();
72 }
73
74
75
76
77 protected static boolean isOverride() {
78 String value = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.LOG4J_OVERRIDE_KEY);
79 return Boolean.parseBoolean(value);
80 }
81
82 protected static File getCustomConfigFile() {
83 String filename = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.LOG4J_SETTINGS_FILE_KEY);
84 if (StringUtils.isBlank(filename)) {
85 throw new IllegalStateException("log4j override requested, but the property " + OLEConstants.LOG4J_SETTINGS_FILE_KEY + " is blank");
86 }
87 File file = new File(filename);
88 if (!file.exists()) {
89 throw new IllegalStateException("[" + OLEConstants.LOG4J_SETTINGS_FILE_KEY + "=" + filename + "], but " + filename + " does not exist.");
90 }
91 if (!file.canRead()) {
92 throw new IllegalStateException("[" + OLEConstants.LOG4J_SETTINGS_FILE_KEY + "=" + filename + "], but " + filename + " is not readable.");
93 }
94 return file;
95 }
96
97 protected static long getReloadMillis() {
98 String s = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.LOG4J_RELOAD_MINUTES_KEY);
99 try {
100 Double minutes = new Double(s);
101 Double millis = minutes * MILLISECONDS_CONVERSION_MULTIPLIER;
102 return millis.longValue();
103 }
104 catch (NumberFormatException e) {
105 throw new IllegalStateException("Could not parse '" + s + "'", e);
106 }
107 }
108
109 protected static void debugClasspath() {
110 URLClassLoader ucl = (URLClassLoader) Thread.currentThread().getContextClassLoader();
111 URL[] urls = ucl.getURLs();
112 List<String> files = new ArrayList<String>();
113 for (URL url : urls) {
114 files.add(url.getFile());
115 }
116 Collections.sort(files);
117 logger.debug("Located " + files.size() + " classpath entries");
118 for (String file : files) {
119 logger.debug("Classpath entry: " + file);
120 }
121 }
122
123 protected static NumberFormat getNumberFormatter() {
124 NumberFormat nf = NumberFormat.getInstance();
125 nf.setGroupingUsed(false);
126 nf.setMaximumFractionDigits(1);
127 nf.setMinimumFractionDigits(1);
128 return nf;
129 }
130
131 }