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

查看:162
本文介绍了重定向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类有一个 setOut setErr 可用于将输出流更改为,例如,新的 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 -type语句。

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天全站免登陆