HADOOP-14706. Adding a helper method to determine whether a log is Log4j implement.

This closes #257

Signed-off-by: Akira Ajisaka <aajisaka@apache.org>
This commit is contained in:
wenxinhe 2017-08-01 16:19:00 +08:00 committed by Akira Ajisaka
parent 35dc782923
commit 691bf5ec5d
No known key found for this signature in database
GPG Key ID: C1EDBB9CA400FD50
3 changed files with 21 additions and 1 deletions

View File

@ -179,7 +179,7 @@
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId> <artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>

View File

@ -23,6 +23,9 @@
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.Log4jLoggerAdapter;
/** /**
* Contains utility methods for dealing with Java Generics. * Contains utility methods for dealing with Java Generics.
@ -72,4 +75,16 @@ public static <T> T[] toArray(List<T> list) {
return toArray(getClass(list.get(0)), list); return toArray(getClass(list.get(0)), list);
} }
/**
* Determine whether the log of <code>clazz</code> is Log4j implementation.
* @param clazz a class to be determined
* @return true if the log of <code>clazz</code> is Log4j implementation.
*/
public static boolean isLog4jLogger(Class<?> clazz) {
if (clazz == null) {
return false;
}
Logger log = LoggerFactory.getLogger(clazz);
return log instanceof Log4jLoggerAdapter;
}
} }

View File

@ -131,4 +131,9 @@ public void testGetClass() {
GenericClass.class, c2); GenericClass.class, c2);
} }
public void testIsLog4jLogger() throws Exception {
assertFalse("False if clazz is null", GenericsUtil.isLog4jLogger(null));
assertTrue("The implementation is Log4j",
GenericsUtil.isLog4jLogger(TestGenericsUtil.class));
}
} }