package org.graylog2.log;

import org.apache.log4j.spi.LoggingEvent;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *
 * @author Anton Yakimov
 * @author Jochen Schalanda
 */
public class Log4jVersionChecker {

    private static Method methodGetTimeStamp = null;

    static {
        Method[] declaredMethods = LoggingEvent.class.getDeclaredMethods();
        for(Method m : declaredMethods) {
            if (m.getName().equals("getTimeStamp")) {
                methodGetTimeStamp = m;
                break;
            }
        }
    }

    public static long getTimeStamp(LoggingEvent event) {

        long timeStamp = 0;
        if(methodGetTimeStamp != null) {

            try {
                timeStamp = (Long) methodGetTimeStamp.invoke(event);
            } catch (IllegalAccessException e) {
                // Just return the current timestamp
            } catch (InvocationTargetException e) {
                // Just return the current timestamp
            }
        }

        return timeStamp == 0 ? System.currentTimeMillis() : timeStamp;
    }
}

package org.graylog2.log;

import org.apache.log4j.spi.LoggingEvent;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *
 * @author Anton Yakimov
 * @author Jochen Schalanda
 */
public class Log4jVersionChecker {

    private static boolean hasGetTimeStamp = true;
    private static Method methodGetTimeStamp = null;

    public static long getTimeStamp(LoggingEvent event) {

        long timeStamp = System.currentTimeMillis();

        if(hasGetTimeStamp && methodGetTimeStamp == null) {

            hasGetTimeStamp = false;

            Method[] declaredMethods = event.getClass().getDeclaredMethods();
            for(Method m : declaredMethods) {
                if (m.getName().equals("getTimeStamp")) {
                    methodGetTimeStamp = m;
                    hasGetTimeStamp = true;

                    break;
                }
            }
        }

        if(hasGetTimeStamp) {

            try {
                timeStamp = (Long) methodGetTimeStamp.invoke(event);
            } catch (IllegalAccessException e) {
                // Just return the current timestamp
            } catch (InvocationTargetException e) {
                // Just return the current timestamp
            }
        }

        return timeStamp;
    }
}

--- 
+++ 
@@ -12,29 +12,22 @@
  */
 public class Log4jVersionChecker {
 
-    private static boolean hasGetTimeStamp = true;
     private static Method methodGetTimeStamp = null;
+
+    static {
+        Method[] declaredMethods = LoggingEvent.class.getDeclaredMethods();
+        for(Method m : declaredMethods) {
+            if (m.getName().equals("getTimeStamp")) {
+                methodGetTimeStamp = m;
+                break;
+            }
+        }
+    }
 
     public static long getTimeStamp(LoggingEvent event) {
 
-        long timeStamp = System.currentTimeMillis();
-
-        if(hasGetTimeStamp && methodGetTimeStamp == null) {
-
-            hasGetTimeStamp = false;
-
-            Method[] declaredMethods = event.getClass().getDeclaredMethods();
-            for(Method m : declaredMethods) {
-                if (m.getName().equals("getTimeStamp")) {
-                    methodGetTimeStamp = m;
-                    hasGetTimeStamp = true;
-
-                    break;
-                }
-            }
-        }
-
-        if(hasGetTimeStamp) {
+        long timeStamp = 0;
+        if(methodGetTimeStamp != null) {
 
             try {
                 timeStamp = (Long) methodGetTimeStamp.invoke(event);
@@ -45,6 +38,6 @@
             }
         }
 
-        return timeStamp;
+        return timeStamp == 0 ? System.currentTimeMillis() : timeStamp;
     }
 }
Move looking up getTimestamp method to a static initialization block
to ensure thread safety just in case that Log4j doesn't execute this
code inside a synchronized block