将 STDOUT 复制到文件而不停止其在屏幕上显示
我正在制作的程序设计为无人值守运行,因此我已将 stdout 和 stderr 流重定向到日志文件。虽然这工作没有任何问题,但当我仍在制作和调试软件时,我希望它也显示在屏幕上。这可能吗?
重定向我使用过的流,
System.setErr(logWriter);
System.setOut(logWriter);
谢谢。
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?
To redirect the streams I have used
System.setErr(logWriter);
System.setOut(logWriter);
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的。日志框架(即 log4j)是最好的,
isDebugMode
在开发环境中很方便但是如果您确实需要“tee”您的标准输出,您可以这样做。http://www.exampledepot.com/egs/java.lang/Redirect.html
您很快就会发现
LOG.somelevel(msg)
比System.out.println(msg)
更易于管理。在不部署调试版本的情况下,当事情运行良好时提高日志级别并在运行不佳时降低日志级别是非常好的。Yes. Logging frameworks (i.e. log4j) are best,
isDebugMode
is handy in a development environment but if you really need to "tee" your stdout you can like so.http://www.exampledepot.com/egs/java.lang/Redirect.html
You'll soon find
LOG.somelevel(msg)
much more manageable thanSystem.out.println(msg)
. It is great to raise the log level when things are working well and lower the level when they aren't without deploying a debug build.也许有点粗糙,但你可以尝试这个:
或者你可以编写自己的 PrintStream 实现,它同时写入日志文件和屏幕。听起来除了在开发中之外,您不需要这种行为,因此后者实际上对您的问题的更准确答案可能不是您真正想要的。
a bit crude perhaps, but you could try this:
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.
如果您使用的是类 Unix 平台(除 Windows 之外的任何平台),您可以使用
tee
程序:这会将标准输出写入控制台以及名为
output
的文件。If you're on a Unix-like platform (anything except Windows) you can use the
tee
program:This will write the standard output to the console as well as the file called
output
.不,根据您的设置,这是不可能的。可能的解决方案:
less
有一个“跟随”模式 (Shift-F),用于跟踪日志文件。其他系统应该有类似的代码。这样做的优点是在调试和调试中使用相同的设置。生产。No, with your setup this is not possible. Possible solutions:
less
has a "follow"-mode (Shift-F), which tracks the log file. Other systems should have similar code. This has the advantage of using the same setup in debugging & production.如果您使用的是 UNIX,您可以使用
tail -f logfile
来查看写入日志文件的行If you're on UNIX you could use
tail -f logfile
to see lines as they are written to your logfile如果你不需要合适的日志框架,
标准 Unix 方法是重定向输出流
,即运行程序 exex
执行程序>日志
将重定向标准输出到日志
但
EXE文件
其本身会将输出留在控制台。
如果您想在程序中执行此操作,则需要一个变量来测试,并且仅在不测试时重定向流
If you don't want a proper logging framework
Standard Unix way is to redirect the output streams
ie run the program exex as
exe > log
will redirect stdout to log
but
exe
on its own will leave the output to the console.
If you want to do this in the program you would need a variable to test on and only redirect the streams if not testing
在 Linux 和 Windows(安装了 cygwin)上,我总是使用 log4j 来记录到文件,然后使用“tail”来显示它。默认情况下, tail -f (和 less -F)每秒更新一次,我发现这太慢了。此外,通常还有几个有趣的日志文件值得查看,其中一些日志文件的名称中包含日期。这是我在我的一个系统上使用的命令:
这同时跟踪 /var/log/myapp/ 下的每个日志文件,其中文件名中包含今天的日期。使用 log4j 滚动日志文件非常方便。 -s 0.05 表示在检查新输出之间仅暂停 0.05 秒。
On both Linux and Windows (with cygwin installed) I always use log4j to log to a file and then use "tail" to display it. By default, tail -f (and less -F) update every second which I find too slow. Also, there are often several interesting log files that are worth looking at, and some of them include the date as part of their name. Here's the command I use on one of my systems:
This simultaneously tails each log file under /var/log/myapp/ which contains today's date in the file name. Very handy with log4j rolling log files. And -s 0.05 means only pause 0.05 seconds between checks for new output.