HADOOP-11430. Add GenericTestUtils#disableLog, GenericTestUtils#setLogLevel (cmccabe)

This commit is contained in:
Colin Patrick Mccabe 2014-12-18 19:39:04 -08:00
parent efe6357748
commit 6635ccd217
6 changed files with 67 additions and 33 deletions

View File

@ -432,6 +432,9 @@ Release 2.7.0 - UNRELEASED
HADOOP-11427. ChunkedArrayList: fix removal via iterator and implement get
(cmccabe)
HADOOP-11430. Add GenericTestUtils#disableLog, GenericTestUtils#setLogLevel
(cmccabe)
OPTIMIZATIONS
HADOOP-11323. WritableComparator#compare keeps reference to byte array.

View File

@ -28,6 +28,7 @@ import org.junit.Before;
import org.junit.Test;
import static org.apache.hadoop.fs.FileContextTestHelper.*;
import org.apache.commons.logging.impl.Log4JLogger;
import org.apache.hadoop.test.GenericTestUtils;
/**
* <p>
@ -54,14 +55,8 @@ public abstract class FileContextCreateMkdirBaseTest {
protected final FileContextTestHelper fileContextTestHelper;
protected static FileContext fc;
{
try {
((Log4JLogger)FileSystem.LOG).getLogger().setLevel(Level.DEBUG);
}
catch(Exception e) {
System.out.println("Cannot change log level\n"
+ StringUtils.stringifyException(e));
}
static {
GenericTestUtils.setLogLevel(FileSystem.LOG, Level.DEBUG);
}
public FileContextCreateMkdirBaseTest() {

View File

@ -38,6 +38,8 @@ import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.util.StringUtils;
import org.apache.hadoop.util.Time;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.WriterAppender;
import org.junit.Assert;
@ -55,6 +57,46 @@ public abstract class GenericTestUtils {
private static final AtomicInteger sequence = new AtomicInteger();
@SuppressWarnings("unchecked")
public static void disableLog(Log log) {
// We expect that commons-logging is a wrapper around Log4j.
disableLog((Log4JLogger) log);
}
public static Logger toLog4j(org.slf4j.Logger logger) {
return LogManager.getLogger(logger.getName());
}
public static void disableLog(Log4JLogger log) {
log.getLogger().setLevel(Level.OFF);
}
public static void disableLog(Logger logger) {
logger.setLevel(Level.OFF);
}
public static void disableLog(org.slf4j.Logger logger) {
disableLog(toLog4j(logger));
}
@SuppressWarnings("unchecked")
public static void setLogLevel(Log log, Level level) {
// We expect that commons-logging is a wrapper around Log4j.
setLogLevel((Log4JLogger) log, level);
}
public static void setLogLevel(Log4JLogger log, Level level) {
log.getLogger().setLevel(level);
}
public static void setLogLevel(Logger logger, Level level) {
logger.setLevel(level);
}
public static void setLogLevel(org.slf4j.Logger logger, Level level) {
setLogLevel(toLog4j(logger), level);
}
/**
* Extracts the name of the method where the invocation has happened
* @return String name of the invoking method

View File

@ -687,11 +687,6 @@ public class DFSTestUtil {
return ((DFSOutputStream) out.getWrappedStream()).getBlockToken();
}
static void setLogLevel2All(org.apache.commons.logging.Log log) {
((org.apache.commons.logging.impl.Log4JLogger)log
).getLogger().setLevel(org.apache.log4j.Level.ALL);
}
public static String readFile(File f) throws IOException {
StringBuilder b = new StringBuilder();
BufferedReader in = new BufferedReader(new FileReader(f));

View File

@ -52,6 +52,8 @@ import org.apache.hadoop.test.PathUtils;
import org.apache.hadoop.util.ReflectionUtils;
import org.apache.hadoop.util.StringUtils;
import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.test.GenericTestUtils;
import org.apache.log4j.Level;
import org.junit.Test;
import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.FS_TRASH_INTERVAL_KEY;
@ -1515,7 +1517,7 @@ public class TestDFSShell {
@Test (timeout = 30000)
public void testGet() throws IOException {
DFSTestUtil.setLogLevel2All(FSInputChecker.LOG);
GenericTestUtils.setLogLevel(FSInputChecker.LOG, Level.ALL);
final String fname = "testGet.txt";
Path root = new Path("/test/get");

View File

@ -62,6 +62,7 @@ import org.apache.hadoop.http.HttpServer2;
import org.apache.hadoop.ipc.ProtobufRpcEngine.Server;
import org.apache.hadoop.metrics2.impl.MetricsSystemImpl;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.test.GenericTestUtils;
import org.apache.log4j.Level;
import org.junit.Assert;
@ -79,28 +80,24 @@ public class SnapshotTestHelper {
"org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.FsDatasetAsyncDiskService",
};
for(String n : lognames) {
setLevel2OFF(LogFactory.getLog(n));
GenericTestUtils.disableLog(LogFactory.getLog(n));
}
setLevel2OFF(LogFactory.getLog(UserGroupInformation.class));
setLevel2OFF(LogFactory.getLog(BlockManager.class));
setLevel2OFF(LogFactory.getLog(FSNamesystem.class));
setLevel2OFF(LogFactory.getLog(DirectoryScanner.class));
setLevel2OFF(LogFactory.getLog(MetricsSystemImpl.class));
GenericTestUtils.disableLog(LogFactory.getLog(UserGroupInformation.class));
GenericTestUtils.disableLog(LogFactory.getLog(BlockManager.class));
GenericTestUtils.disableLog(LogFactory.getLog(FSNamesystem.class));
GenericTestUtils.disableLog(LogFactory.getLog(DirectoryScanner.class));
GenericTestUtils.disableLog(LogFactory.getLog(MetricsSystemImpl.class));
setLevel2OFF(DataBlockScanner.LOG);
setLevel2OFF(HttpServer2.LOG);
setLevel2OFF(DataNode.LOG);
setLevel2OFF(BlockPoolSliceStorage.LOG);
setLevel2OFF(LeaseManager.LOG);
setLevel2OFF(NameNode.stateChangeLog);
setLevel2OFF(NameNode.blockStateChangeLog);
setLevel2OFF(DFSClient.LOG);
setLevel2OFF(Server.LOG);
}
static void setLevel2OFF(Object log) {
((Log4JLogger)log).getLogger().setLevel(Level.OFF);
GenericTestUtils.disableLog(DataBlockScanner.LOG);
GenericTestUtils.disableLog(HttpServer2.LOG);
GenericTestUtils.disableLog(DataNode.LOG);
GenericTestUtils.disableLog(BlockPoolSliceStorage.LOG);
GenericTestUtils.disableLog(LeaseManager.LOG);
GenericTestUtils.disableLog(NameNode.stateChangeLog);
GenericTestUtils.disableLog(NameNode.blockStateChangeLog);
GenericTestUtils.disableLog(DFSClient.LOG);
GenericTestUtils.disableLog(Server.LOG);
}
private SnapshotTestHelper() {