Skip to contentSkip to conversation

Fix: Abort loading log4j agent classes when detected log4j version is too old

PATCH Mathieu Desnoyers 1 participant 1 message Oct 2, 2014 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Mathieu Desnoyers

author Applied
#1 lore raw

Merged, thanks!

Mathieu

----- Original Message -----

Quoted 87 lines

From: "Christian Babeux" <[email protected]>
To: "mathieu desnoyers" <[email protected]>
Cc: [email protected], "Christian Babeux" <[email protected]>
Sent: Thursday, October 2, 2014 4:31:56 PM
Subject: [PATCH] Fix: Abort loading log4j agent classes when detected log4j version is too old

The Java Log4j agent rely on the getTimeStamp() method that was introduced in log4j 1.2.15. This fix implement a runtime detection of the log4j library capabilities and abort the loading of the relevant agent classes if the version used is too old.

Thus, log4j tracing will be deactivated on older version of the library.

---
 .../java/org/lttng/ust/agent/LTTngAgent.java       | 38
 ++++++++++++++++++----
 1 file changed, 32 insertions(+), 6 deletions(-)
liblttng-ust-java-agent/java/org/lttng/ust/agent/LTTngAgent.java +32 -7
index 342ccfa..293ac84 100644
@@ -29,7 +29,6 @@ import java.util.Enumeration;
 import java.lang.reflect.InvocationTargetException;
 
 import java.util.logging.Logger;
-import java.util.logging.FileHandler;;
 import java.util.logging.SimpleFormatter;
 
 public class LTTngAgent {
@@ -92,17 +91,44 @@ public class LTTngAgent {
 	}
 
 	private Boolean loadLog4jClasses() {
-		Boolean loaded = false;
+		Class<?> logging;
+
 		try {
 			ClassLoader loader = ClassLoader.getSystemClassLoader();
-			loader.loadClass("org.apache.log4j.Logger");
-			loaded = true;
+			logging = loader.loadClass("org.apache.log4j.spi.LoggingEvent");
 		} catch (ClassNotFoundException e) {
 			/* Log4j classes not found, no need to create the relevant objects */
-			loaded = false;
+			return false;
+		}
+
+		/*
+		 * Detect capabilities of the log4j library. We only
+		 * support log4j >= 1.2.15.  The getTimeStamp() method
+		 * was introduced in log4j 1.2.15, so verify that it
+		 * is available.
+		 *
+		 * We can't rely on the getPackage().getImplementationVersion()
+		 * call that would retrieves information from the manifest file
+		 * found in the JAR since the manifest file shipped
+		 * from upstream is known to be broken in several
+		 * versions of the library.
+		 *
+		 * More info:
+		 * https://issues.apache.org/bugzilla/show_bug.cgi?id=44370
+		 */
+
+		try {
+			logging.getDeclaredMethod("getTimeStamp");
+		} catch (NoSuchMethodException e) {
+			return false;
+		} catch (NullPointerException e) {
+			/* Should never happen */
+			return false;
+		} catch (SecurityException e) {
+			return false;
 		}
 
-		return loaded;
+		return true;
 	}
 
 	private void initAgentJULClasses() {
--

2.1.1

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com