logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1178261 - in /logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src: main/java/org/apache/logging/log4j/core/ main/java/org/apache/logging/log4j/core/appender/rolling/ main/java/org/apache/logging/log4j/core/layout/ test/ja...
Date Sun, 02 Oct 2011 22:00:41 GMT
Author: rgoers
Date: Sun Oct  2 22:00:41 2011
New Revision: 1178261

URL: http://svn.apache.org/viewvc?rev=1178261&view=rev
Log:
Add OnStartupTriggeringPolicy

Added:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
Modified:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java?rev=1178261&r1=1178260&r2=1178261&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
Sun Oct  2 22:00:41 2011
@@ -36,8 +36,6 @@ public class LoggerContext implements or
 
     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 @@ public class LoggerContext implements or
     }
 
     /**
-     * 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.
      */

Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java?rev=1178261&view=auto
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java
(added)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java
Sun Oct  2 22:00:41 2011
@@ -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();
+    }
+}

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java?rev=1178261&r1=1178260&r2=1178261&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
Sun Oct  2 22:00:41 2011
@@ -30,6 +30,7 @@ import java.io.LineNumberReader;
 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 @@ public class HTMLLayout extends Abstract
 
     protected static final int BUF_SIZE = 256;
 
-    private static final String TRACE_PREFIX = "<br>&nbsp;&nbsp;&nbsp;&nbsp;";
-
     // 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>&nbsp;&nbsp;&nbsp;&nbsp;";
+
     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 @@ public class HTMLLayout extends Abstract
         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());

Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java?rev=1178261&view=auto
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
(added)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
Sun Oct  2 22:00:41 2011
@@ -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;
+        }
+    }
+}



Mime
View raw message