欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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));

相关标签: log4j