log4j屏蔽system.out的方法 摘取
程序员文章站
2022-07-06 09:56:03
...
public static void setStdoutWithLogger(String logName)
{
System.setOut(new LoggerPrintStream(logName));
}
class LoggerPrintStream extends PrintStream{
Logger logger;
public LoggerPrintStream(String logName)
{
super(new ByteArrayOutputStream(0));
logger = Logger.getLogger(logName);
if( logger == null)
throw new RuntimeException("Can't logger:"+pLoggerName);
}
public void println(String s) {
log(s,level);
}
///其它它代码略.......
}
核心代码 System.setOut(new LoggerPrintStream(logName));
上一篇: Object类型的参数
下一篇: printStackTrace 实现