logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rpo...@apache.org
Subject svn commit: r1525494 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java src/changes/changes.xml src/site/xdoc/manual/appenders.xml
Date Mon, 23 Sep 2013 03:07:15 GMT
Author: rpopma
Date: Mon Sep 23 03:07:14 2013
New Revision: 1525494

URL: http://svn.apache.org/r1525494
Log:
LOG4J2-379: Fixed issue that prevented Log4J from working in Google App Engine

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml
    logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java?rev=1525494&r1=1525493&r2=1525494&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicy.java
Mon Sep 23 03:07:14 2013
@@ -16,9 +16,8 @@
  */
 package org.apache.logging.log4j.core.appender.rolling;
 
-import java.lang.management.ManagementFactory;
+import java.lang.reflect.Method;
 
-import org.apache.logging.log4j.Logger;
 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;
@@ -29,25 +28,13 @@ import org.apache.logging.log4j.status.S
  * and if it is older isTriggeringEvent will return true. After isTriggeringEvent has been
called it will
  * always return false.
  */
-
 @Plugin(name = "OnStartupTriggeringPolicy", category = "Core", printObject = true)
 public class OnStartupTriggeringPolicy implements TriggeringPolicy {
-    private static long JVM_START_TIME = ManagementFactory.getRuntimeMXBean().getStartTime();
-
-    private static final Logger LOGGER = StatusLogger.getLogger();
+    private static long JVM_START_TIME = initStartTime();
 
     private boolean evaluated = false;
-
     private RollingFileManager manager;
 
-    /* static {
-        try {
-            JVM_START_TIME = ManagementFactory.getRuntimeMXBean().getStartTime();
-        } catch (Exception ex) {
-            LOGGER.error("Unable to calculate JVM start time - {}", ex.getMessage());
-        }
-    } */
-
     /**
      * Provide the RollingFileManager to the policy.
      * @param manager The RollingFileManager.
@@ -61,6 +48,35 @@ public class OnStartupTriggeringPolicy i
     }
 
     /**
+     * Returns the result of {@code ManagementFactory.getRuntimeMXBean().getStartTime()},
+     * or the current system time if JMX is not available.
+     */
+    private static long initStartTime() {
+        // LOG4J2-379:
+        // We'd like to call ManagementFactory.getRuntimeMXBean().getStartTime(),
+        // but Google App Engine throws a java.lang.NoClassDefFoundError
+        // "java.lang.management.ManagementFactory is a restricted class".
+        // The reflection is necessary because without it, Google App Engine
+        // will refuse to initialize this class.
+        try {
+            Class<?> factoryClass = Class.forName("java.lang.management.ManagementFactory");
+            Method getRuntimeMXBean = factoryClass.getMethod("getRuntimeMXBean", new Class[0]);
+            Object runtimeMXBean = getRuntimeMXBean.invoke(null, new Object[0]);
+            
+            Class<?> runtimeMXBeanClass = Class.forName("java.lang.management.RuntimeMXBean");
+            Method getStartTime = runtimeMXBeanClass.getMethod("getStartTime", new Class[0]);
+            Long result = (Long) getStartTime.invoke(runtimeMXBean, new Object[0]);
+            
+            return result.longValue();
+        } catch (Throwable t) {
+            StatusLogger.getLogger().error("Unable to call ManagementFactory.getRuntimeMXBean().getStartTime(),
" //
+                    + "using system time for OnStartupTriggeringPolicy", t);
+            // We have little option but to declare "now" as the beginning of time.
+            return System.currentTimeMillis();
+        }
+    }
+
+    /**
      * 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.

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1525494&r1=1525493&r2=1525494&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Mon Sep 23 03:07:14 2013
@@ -21,6 +21,9 @@
   </properties>
   <body>
     <release version="2.0RC1" date="2013-MM-DD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-379" dev="rpopma" type="fix">
+        Fixed issue that prevented Log4J from working in Google App Engine.
+      </action>
       <action issue="LOG4J2-401" dev="ggregory" type="add">
         Configure FileAppender buffer size.
       </action>

Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml?rev=1525494&r1=1525493&r2=1525494&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml Mon Sep 23 03:07:14 2013
@@ -2246,6 +2246,14 @@ public class JpaLogEntity extends Abstra
                 The OnStartup policy takes no parameters and causes a rollover if the log
file is older than the
                 current JVM's start time.
               </p>
+              <p>
+                <em>Google App Engine note:</em><br />
+                When running in Google App Engine, the OnStartup policy causes a rollover
if the log file is older
+                than <em>the time when Log4J initialized</em>.
+                (Google App Engine restricts access to certain classes so Log4J cannot determine
JVM start time with 
+                <code>java.lang.management.ManagementFactory.getRuntimeMXBean().getStartTime()</code>
+                and falls back to Log4J initialization time instead.)
+              </p>
             <h5>SizeBased Triggering Policy</h5>
               <p>
                 Causes a rollover once the file has reached the specified size. The size
can be specified in bytes,



Mime
View raw message