重定向 System.out.println [英] Redirect System.out.println

查看:44
本文介绍了重定向 System.out.println的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有很多 System.out.println() 语句.

My application has many System.out.println() statements.

我想从 println 捕获消息并将它们发送到标准记录器(Log4j、JUL 等).

I want to catch messages from println and send them to the standard logger (Log4j, JUL etc).

怎么做?

推荐答案

System 类有一个 setOutsetErr 可用于将输出流更改为,例如,一个带有支持 File 的新 PrintStream,或者,在这种情况下,可能是另一个使用您选择的日志子系统的流.

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.

请记住,如果您将日志库配置为输出到标准输出或错误(可能是无限递归类型),您很可能会遇到麻烦.

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

如果是这种情况,您可能只想用真正的日志调用替换 System.out.print 类型的语句.

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.println的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆