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;
    }
}

@@ -272,310 +272,56 @@
 tic 
-boolean hasGetTimeStamp = true;%0A    private static Method methodGetTimeStamp = null;%0A%0A    public static long getTimeStamp(LoggingEvent event) %7B%0A%0A        long timeStamp = System.currentTimeMillis();%0A%0A        if(hasGetTimeStamp && methodGetTimeStamp == null) %7B%0A%0A            hasGetTimeStamp = false;%0A%0A    
+Method methodGetTimeStamp = null;%0A%0A    static %7B%0A
     
@@ -355,24 +355,26 @@
 s = 
-e
+LoggingE
 vent.
-getC
+c
 lass
-()
 .get
@@ -392,20 +392,16 @@
 hods();%0A
-    
         
@@ -446,20 +446,16 @@
         
-    
 if (m.ge
@@ -504,20 +504,16 @@
         
-    
 methodGe
@@ -548,73 +548,37 @@
     
-    hasGetTimeStamp = true;%0A%0A                    break;%0A 
+break;%0A            %7D%0A
         
     
@@ -577,41 +577,103 @@
     
-  
+%7D%0A
     
- 
 %7D
+%0A
 %0A    
-     
+public static long getTimeStamp(LoggingEvent event) %7B%0A%0A
    
-%7D%0A
      
-   %7D%0A
+long timeStamp = 0;
 %0A   
@@ -684,11 +684,14 @@
  if(
-has
+method
 GetT
@@ -698,16 +698,24 @@
 imeStamp
+ != null
 ) %7B%0A%0A   
@@ -1044,16 +1044,62 @@
   return
+ timeStamp == 0 ? System.currentTimeMillis() :
  timeSta

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