Add FileMonitor. Remove max window size from RolloverStrategy
git-svn-id: https://svn.apache.org/repos/asf/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers@1153584 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java b/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
index d7232d2..1b49770 100644
--- a/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
+++ b/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
@@ -61,10 +61,6 @@
*/
@Plugin(name = "DefaultRolloverStrategy", type = "Core", printObject = true)
public class DefaultRolloverStrategy implements RolloverStrategy {
- /**
- * It's almost always a bad idea to have a large window size, say over 12.
- */
- private static final int MAX_WINDOW_SIZE = 12;
private static final int MIN_WINDOW_SIZE = 1;
private static final int DEFAULT_WINDOW_SIZE = 7;
@@ -222,17 +218,8 @@
@PluginFactory
public static DefaultRolloverStrategy createStrategy(@PluginAttr("max") String max,
- @PluginAttr("min") String min) {
- int maxIndex;
- if (max != null) {
- maxIndex = Integer.parseInt(max);
- if (maxIndex > MAX_WINDOW_SIZE) {
- logger.error("Maximum window size too large. Limited to " + MAX_WINDOW_SIZE);
- maxIndex = MAX_WINDOW_SIZE;
- }
- } else {
- maxIndex = DEFAULT_WINDOW_SIZE;
- }
+ @PluginAttr("min") String min) {
+
int minIndex;
if (min != null) {
minIndex = Integer.parseInt(min);
@@ -243,7 +230,16 @@
} else {
minIndex = MIN_WINDOW_SIZE;
}
-
+ int maxIndex;
+ if (max != null) {
+ maxIndex = Integer.parseInt(max);
+ if (maxIndex < minIndex) {
+ maxIndex = minIndex < DEFAULT_WINDOW_SIZE ? DEFAULT_WINDOW_SIZE : minIndex;
+ logger.error("Maximum window size must be greater than the minimum windows size. Set to " + maxIndex);
+ }
+ } else {
+ maxIndex = DEFAULT_WINDOW_SIZE;
+ }
return new DefaultRolloverStrategy(minIndex, maxIndex);
}
diff --git a/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/FileConfigurationMonitor.java b/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/FileConfigurationMonitor.java
index a372183..b62c753 100644
--- a/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/FileConfigurationMonitor.java
+++ b/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/FileConfigurationMonitor.java
@@ -24,16 +24,45 @@
*/
public class FileConfigurationMonitor implements ConfigurationMonitor {
- private File file;
+ private final File file;
- private List<ConfigurationListener> listeners;
+ private long lastModified;
- public FileConfigurationMonitor(File file, List<ConfigurationListener> listeners) {
+ private final List<ConfigurationListener> listeners;
+
+ private final int interval;
+
+ private long nextCheck;
+
+ private final static int MIN_INTERVAL = 30;
+
+ private volatile int counter = 0;
+ private static final int MASK = 0x0f;
+ private long start;
+
+ public FileConfigurationMonitor(File file, List<ConfigurationListener> listeners, int interval) {
this.file = file;
+ this.lastModified = file.lastModified();
this.listeners = listeners;
+ this.interval = (interval < MIN_INTERVAL ? MIN_INTERVAL : interval) * 1000;
+ this.nextCheck = System.currentTimeMillis() + interval;
+ this.start = System.nanoTime();
}
public void checkConfiguration() {
-
+ if ((++counter & MASK) == 0) {
+ long current = System.currentTimeMillis();
+ synchronized(this) {
+ if (current >= nextCheck) {
+ nextCheck = current + interval;
+ if (lastModified >= file.lastModified()) {
+ lastModified = file.lastModified();
+ for (ConfigurationListener listener : listeners) {
+ listener.onChange();
+ }
+ }
+ }
+ }
+ }
}
}
diff --git a/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java b/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
index 014b95d..fff2bad 100644
--- a/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
+++ b/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
@@ -98,7 +98,7 @@
} else if ("monitorInterval".equalsIgnoreCase(entry.getKey())) {
int interval = Integer.parseInt(entry.getValue());
if (interval > 0 && configFile != null) {
- monitor = new FileConfigurationMonitor(configFile, listeners);
+ monitor = new FileConfigurationMonitor(configFile, listeners, interval);
}
}
}