Java 重定向 System.out.println
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3228427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect System.out.println
提问by EK.
My application has many System.out.println() statements.
我的应用程序有很多 System.out.println() 语句。
I want to catch messages from println and send them to the standard logger (Log4j, JUL etc).
我想从 println 捕获消息并将它们发送到标准记录器(Log4j、JUL 等)。
How to do that ?
怎么做 ?
采纳答案by paxdiablo
The System class has a setOut
and setErr
that can be used to change the output stream to, for example, a new PrintStream
with a backing File
or, in this case, probably another stream which uses your logging subsystem of choice.
System 类有一个setOut
和setErr
,可用于将输出流更改为例如PrintStream
具有支持的新流,File
或者在这种情况下可能是另一个使用您选择的日志记录子系统的流。
Keep in mind you may well get yourself into trouble if you ever configure your logging library to output to standard output or error (of the infinite recursion type, possibly).
请记住,如果您曾经将日志库配置为输出到标准输出或错误(可能是无限递归类型),那么您很可能会遇到麻烦。
If that's the case, you may want to just go and replace your System.out.print
-type statements with real logging calls.
如果是这种情况,您可能只想System.out.print
用真正的日志记录调用替换您的-type 语句。
回答by Stefan Kendall
The better solution is to go through and change all the println statements to use a proper logging library. What you're trying to do is a big hack.
更好的解决方案是遍历并更改所有 println 语句以使用适当的日志记录库。你想要做的是一个大黑客。
回答by Wizard of Kneup
I had a similar need once. I needed to intercept the output of some 3rd party component and react on a error message. The concept looks like this:
我曾经有过类似的需求。我需要拦截某些 3rd 方组件的输出并对错误消息做出反应。这个概念看起来像这样:
private class Interceptor extends PrintStream
{
public Interceptor(OutputStream out)
{
super(out, true);
}
@Override
public void print(String s)
{//do what ever you like
super.print(s);
}
}
public static void main(String[] args)
{
PrintStream origOut = System.out;
PrintStream interceptor = new Interceptor(origOut);
System.setOut(interceptor);// just add the interceptor
}
回答by froes
I applied the base idea of this and it workes fine. No need to change all the System.out.### and System.err.### stuff.
我应用了这个基本思想,它工作得很好。无需更改所有 System.out.### 和 System.err.### 内容。
import java.io.OutputStream;
import java.io.PrintStream;
public class Interceptor extends PrintStream
{
/** the logger */
private Logger log;
/** the origin output stream */
PrintStream orig;
/**
* Initializes a new instance of the class Interceptor.
*
* @param out the output stream to be assigned
* @param log the logger
*/
public Interceptor( OutputStream out, Logger log )
{
super( out, true );
this.log = log;
}
/**
* {@inheritDoc}
*/
@Override
protected void finalize() throws Throwable
{
detachOut();
super.finalize();
}
/**
* {@inheritDoc}
*/
@Override
public void print( String s )
{
//do what ever you like
orig.print( s );
log.logO( s, true );
}
/**
* {@inheritDoc}
*/
@Override
public void println( String s )
{
print( s + Defines.LF_GEN );
}
/**
* Attaches System.out to interceptor.
*/
public void attachOut()
{
orig = System.out;
System.setOut( this );
}
/**
* Attaches System.err to interceptor.
*/
public void attachErr()
{
orig = System.err;
System.setErr( this );
}
/**
* Detaches System.out.
*/
public void detachOut()
{
if( null != orig )
{
System.setOut( orig );
}
}
/**
* Detaches System.err.
*/
public void detachErr()
{
if( null != orig )
{
System.setErr( orig );
}
}
}
public class InterceptionManager
{
/** out */
private Interceptor out;
/** err */
private Interceptor err;
/** log */
private Logger log;
/**
* Initializes a new instance of the class InterceptionManager.
*
* @param logFileName the log file name
* @param append the append flag
*/
public InterceptionManager( String logFileName, boolean append )
{
log = new Logger();
log.setLogFile( logFileName, append );
this.out = new Interceptor( System.out, log );
this.out.attachOut();
this.err = new Interceptor( System.err, log );
this.err.attachErr();
}
/**
* {@inheritDoc}
*/
@Override
protected void finalize() throws Throwable
{
if( null != log )
{
log.closeLogFile();
}
super.finalize();
}
}
This view lines will enable logging without further code changes:
此视图行将启用日志记录,而无需进一步更改代码:
if( writeLog )
{
logFileName = this.getClassName() + "_Log.txt";
icMan = new InterceptionManager( logFileName, false );
System.out.format( "Logging to '%s'\n", logFileName );
}
回答by Vic Seedoubleyew
Here is how to capture prints to System.out, and then put things back in order :
以下是如何将打印件捕获到 System.out,然后按顺序放回原处:
// Start capturing
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
System.setOut(new PrintStream(buffer));
// Run what is supposed to output something
...
// Stop capturing
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
// Use captured content
String content = buffer.toString();
buffer.reset();
回答by sergeych
extending PrintStream is a bad solution as you will have to override all print()
and println()
methods. Instead, you can capture the stream:
扩展 PrintStream 是一个糟糕的解决方案,因为您必须覆盖 allprint()
和println()
方法。相反,您可以捕获流:
public class ConsoleInterceptor {
public interface Block {
void call() throws Exception;
}
public static String copyOut(Block block) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(bos, true);
PrintStream oldStream = System.out;
System.setOut(printStream);
try {
block.call();
}
finally {
System.setOut(oldStream);
}
return bos.toString();
}
}
Now you can capture it like this:
现在你可以像这样捕获它:
String result = ConsoleInterceptor.copyOut(() ->{
System.out.print("hello world");
System.out.print('!');
System.out.println();
System.out.println("foobar");
});
assertEquals("hello world!\nfoobar\n", result);