|
@@ -20,6 +20,7 @@ package org.apache.hadoop.util;
|
|
|
|
|
|
import java.lang.reflect.Array;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
import org.apache.hadoop.classification.InterfaceAudience;
|
|
|
import org.apache.hadoop.classification.InterfaceStability;
|
|
@@ -33,6 +34,14 @@ import org.slf4j.LoggerFactory;
|
|
|
@InterfaceStability.Unstable
|
|
|
public class GenericsUtil {
|
|
|
|
|
|
+ private static final String SLF4J_LOG4J_ADAPTER_CLASS = "org.slf4j.impl.Log4jLoggerAdapter";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Set to false only if log4j adapter class is not found in the classpath. Once set to false,
|
|
|
+ * the utility method should not bother re-loading class again.
|
|
|
+ */
|
|
|
+ private static final AtomicBoolean IS_LOG4J_LOGGER = new AtomicBoolean(true);
|
|
|
+
|
|
|
/**
|
|
|
* Returns the Class object (of type <code>Class<T></code>) of the
|
|
|
* argument of type <code>T</code>.
|
|
@@ -87,12 +96,27 @@ public class GenericsUtil {
|
|
|
if (clazz == null) {
|
|
|
return false;
|
|
|
}
|
|
|
- Logger log = LoggerFactory.getLogger(clazz);
|
|
|
+ return isLog4jLogger(clazz.getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Determine whether the log of the given logger is of Log4J implementation.
|
|
|
+ *
|
|
|
+ * @param logger the logger name, usually class name as string.
|
|
|
+ * @return true if the logger uses Log4J implementation.
|
|
|
+ */
|
|
|
+ public static boolean isLog4jLogger(String logger) {
|
|
|
+ if (logger == null || !IS_LOG4J_LOGGER.get()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Logger log = LoggerFactory.getLogger(logger);
|
|
|
try {
|
|
|
- Class log4jClass = Class.forName("org.slf4j.impl.Log4jLoggerAdapter");
|
|
|
+ Class<?> log4jClass = Class.forName(SLF4J_LOG4J_ADAPTER_CLASS);
|
|
|
return log4jClass.isInstance(log);
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
+ IS_LOG4J_LOGGER.set(false);
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
}
|