将STDOUT复制到文件而不停止显示在屏幕上 [英] Copy STDOUT to file without stopping it showing onscreen

查看:102
本文介绍了将STDOUT复制到文件而不停止显示在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作的程序旨在无人值守运行,因此我已将stdout和stderr流重定向到日志文件。虽然这没有任何问题,但我仍在制作和调试软件,我希望它也能在屏幕上显示。这可能吗?

The program I am making is designed to be run unattended, because of this I have redirected the stdout and stderr streams to a log file. While this works without any problems, while I am still making and debugging the software I would like it to show on the screen as well. Is this possible?

重定向我使用过的流

System.setErr(logWriter);
System.setOut(logWriter);

谢谢。

推荐答案

或许有点粗糙,但你可以试试这个:

a bit crude perhaps, but you could try this:

private static final isDebugMode = true;

...

if (!isDebugMode) {
  System.setErr(logWriter);
  System.setOut(logWriter);
} 

或者你可以编写自己的PrintStream实现,同时写入你的日志文件和屏幕。听起来你不需要这种行为,除了在开发中,所以后者虽然实际上对你的问题更准确的答案可能不是你真正想要的。

Alternatively you could write your own PrintStream implementation which simultaneously writes to both your log file and the screen. It doesn't sound like you need this behaviour except in development though so the latter whilst actually a more accurate answer to your question is probably not what you actually want.

这篇关于将STDOUT复制到文件而不停止显示在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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