Logger logger = Logger.getLogger(Log4jTest.class); public void testSystemout(){ System.out.println("can print syso"); logger.info("log4j test"); } public static class LoggerHiddenSystemOutPrintStream extends PrintStream{ private Logger logger; public LoggerHiddenSystemOutPrintStream() { super(new ByteArrayOutputStream(0)); logger = Logger.getLogger(LoggerHiddenSystemOutPrintStream.class); } @Override public void println(String x) { logger.debug(x); } } @Test public void hiddenSys(){ System.setOut(new LoggerHiddenSystemOutPrintStream()); testSystemout(); }
把这段代码放到 一个junit类中,即可测试了。运用log4j level屏蔽掉 System.out 输出