Add OnStartupTriggeringPolicy
git-svn-id: https://svn.apache.org/repos/asf/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers@1178261 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java b/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
index 7dd7a55..aab9064 100644
--- a/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
+++ b/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
@@ -36,8 +36,6 @@
private static StatusLogger logger = StatusLogger.getLogger();
- private static final long JVM_START_TIME = System.currentTimeMillis();
-
private final ConcurrentMap<String, Logger> loggers = new ConcurrentHashMap<String, Logger>();
/**
@@ -107,14 +105,6 @@
}
/**
- * The time the LoggerContext class was loaded as a long.
- * @return The time the LoggerContext was loaded.
- */
- public static long getStartTime() {
- return JVM_START_TIME;
- }
-
- /**
* Set the external context.
* @param context The external context.
*/
diff --git a/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java b/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java
new file mode 100644
index 0000000..0fc1bd9
--- /dev/null
+++ b/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.appender.rolling;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+
+import java.lang.management.ManagementFactory;
+
+/**
+ * Trigger a rollover on every restart. The target file's timestamp is compared with the JVM start time
+ * and if it is older isTriggeringEvent will return true. After isTriggeringEvent has been called it will
+ * always return false.
+ */
+
+@Plugin(name = "OnStartupTriggeringPolicy", type = "Core", printObject = true)
+public class OnStartupTriggeringPolicy implements TriggeringPolicy {
+
+ private boolean evaluated = false;
+
+ private RollingFileManager manager;
+
+ private static long jvmStartTime = ManagementFactory.getRuntimeMXBean().getStartTime();
+
+ /**
+ * Provide the RollingFileManager to the policy.
+ * @param manager The RollingFileManager.
+ */
+ public void initialize(RollingFileManager manager) {
+ this.manager = manager;
+ }
+
+ /**
+ * Determine if a rollover should be triggered.
+ * @param event A reference to the current event.
+ * @return true if the target file's timestamp is older than the JVM start time.
+ */
+ public boolean isTriggeringEvent(LogEvent event) {
+ if (evaluated) {
+ return false;
+ }
+ evaluated = true;
+ return manager.getFileTime() < jvmStartTime;
+ }
+
+ @Override
+ public String toString() {
+ return "OnStartupTriggeringPolicy";
+ }
+
+ @PluginFactory
+ public static OnStartupTriggeringPolicy createPolicy() {
+ return new OnStartupTriggeringPolicy();
+ }
+}
diff --git a/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java b/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
index 2481295..b52c2e5 100644
--- a/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
+++ b/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
@@ -30,6 +30,7 @@
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
+import java.lang.management.ManagementFactory;
import java.nio.charset.Charset;
import java.util.ArrayList;
@@ -44,20 +45,22 @@
protected static final int BUF_SIZE = 256;
- private static final String TRACE_PREFIX = "<br> ";
-
// Print no location info by default
protected final boolean locationInfo;
+ protected final String title;
+
+ protected final String contentType;
+
+ private static final String TRACE_PREFIX = "<br> ";
+
private static final String LINE_SEP = System.getProperty("line.separator");
private static final String DEFAULT_TITLE = "Log4J Log Messages";
private static final String DEFAULT_CONTENT_TYPE = "text/html";
- protected final String title;
-
- protected final String contentType;
+ private final long jvmStartTime = ManagementFactory.getRuntimeMXBean().getStartTime();
public HTMLLayout(boolean locationInfo, String title, String contentType, Charset charset) {
super(charset);
@@ -72,7 +75,7 @@
sbuf.append(LINE_SEP).append("<tr>").append(LINE_SEP);
sbuf.append("<td>");
- sbuf.append(event.getMillis() - LoggerContext.getStartTime());
+ sbuf.append(event.getMillis() - jvmStartTime);
sbuf.append("</td>").append(LINE_SEP);
String escapedThread = Transform.escapeTags(event.getThreadName());
diff --git a/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java b/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
new file mode 100644
index 0000000..9477877
--- /dev/null
+++ b/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.appender.rolling;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class OnStartupTriggeringPolicyTest {
+
+ @Test
+ public void testPolicy() {
+ MyRollingManager manager = new MyRollingManager();
+ manager.setFileTime(System.currentTimeMillis() - 36000000);
+ OnStartupTriggeringPolicy policy = OnStartupTriggeringPolicy.createPolicy();
+ policy.initialize(manager);
+ LogEvent event = new Log4jLogEvent(null, null, null, Level.ERROR, new SimpleMessage("Test"), null);
+ assertTrue("Expected trigger to succeed", policy.isTriggeringEvent(event));
+ assertTrue("Expected trigger not to fire", !policy.isTriggeringEvent(event));
+ policy = OnStartupTriggeringPolicy.createPolicy();
+ policy.initialize(manager);
+ manager.setFileTime(System.currentTimeMillis());
+ assertTrue("Expected trigger not to fire", !policy.isTriggeringEvent(event));
+
+ }
+
+ private class MyRollingManager extends RollingFileManager {
+
+ private long timestamp;
+
+ public MyRollingManager() {
+ super("testfile", "target/rolling1/test1-%i.log.gz", new ByteArrayOutputStream(),
+ false, 0, System.currentTimeMillis());
+ }
+
+ public void setFileTime(long timestamp) {
+ this.timestamp = timestamp;
+ }
+
+ public long getFileTime() {
+ return timestamp;
+ }
+ }
+}