log4j屏蔽system.out的方法 摘取

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

你可能感兴趣的:(java)