Ralph (and others),
On Sat, Jan 14, 2012 at 11:31 PM, Ralph Goers
<[email protected]> wrote:
> I really don't think it is worth upgrading Log4j 1.x. I'm sure it is used in
> a lot of places because it supports old JDKs where SLF4J and Logback can't be
> used.
>
> Log4j 2.0 has a minimum version of Java 5. It really is at the point where it
> should be moved out of my private branch anyway. At this point I'm working
> on documentation and a large part of that is written.
>
> What you can do is have a method somewhere that returns the Java version as
> an int and then test for javaVersion >= 5 before doing the remove. Then you
> would require Java 5 to compile but not to run. To compile with a lesser
> version you'd have to use Method.invoke(). IIRC Curt did something similar
> to that in ExtendedThrowableRenderer to take advantage of being able to get
> access to StackTraceElements instead of parsing the stack trace.
My feeling says this problem should have occured when running tests or
compiling on my environment. I now wrote a fix which seems to work,
but would love if you could have look on it before I commit. Basically
I do the same as Curt did, with reflection.
Cheers
Index: src/main/java/org/apache/log4j/MDC.java
===================================================================
--- src/main/java/org/apache/log4j/MDC.java (revision 1231361)
+++ src/main/java/org/apache/log4j/MDC.java (working copy)
@@ -17,6 +17,8 @@
package org.apache.log4j;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.util.Hashtable;
import org.apache.log4j.helpers.Loader;
import org.apache.log4j.helpers.ThreadLocalMap;
@@ -49,13 +51,21 @@
boolean java1;
Object tlm;
-
+
+ Method removeMethod;
+
private
MDC() {
java1 = Loader.isJava1();
if(!java1) {
tlm = new ThreadLocalMap();
}
+
+ try {
+ removeMethod = ThreadLocal.class.getMethod("remove", null);
+ } catch (NoSuchMethodException e) {
+ // don't do anything - java prior 1.5
+ }
}
/**
@@ -181,7 +191,16 @@
if(ht != null) {
ht.clear();
}
- ((ThreadLocalMap)tlm).remove();
+ if(removeMethod != null) {
+ // java 1.3/1.4 does not have remove - will suffer from a memory leak
+ try {
+ removeMethod.invoke(tlm, null);
+ } catch (IllegalAccessException e) {
+ // should not happen
+ } catch (InvocationTargetException e) {
+ // should not happen
+ }
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]