使用 grep 捕获 log4J 输出

发布于 2024-09-05 13:50:16 字数 168 浏览 3 评论 0原文

我知道 log4j 默认输出到 stderror。

我一直使用以下命令捕获应用程序的输出:

application_to_run 2> log ; cat log | grep FATAL

有没有办法在没有辅助文件的情况下捕获输出?

I know that log4j by default outputs to stderror.

I have been capturing the out put of my application with the following command:

application_to_run 2> log ; cat log | grep FATAL

Is there a way to capture the output without the auxiliary file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

用心笑 2024-09-12 13:50:16

如果您想要同时使用 stdoutstderr,请使用:

( application_to_run 2>&1 ) | grep FATAL

如果您想要同时使用 stderr,则可以使用:

( application_to_run 2>&1 >/dev/null ) | grep FATAL

第一个将所有输出发送到文件将句柄 2 (stderr) 传输到文件句柄 1 (stdout),然后通过 grep 进行管道传输。第二个执行相同的操作,但还将 stdout 发送到位存储桶。这将起作用,因为重定向是一个位置问题。首先,stderr 被重定向到当前 stdout,然后 stdout 被重定向到 /dev/null

If you want both stdout and stderr, use:

( application_to_run 2>&1 ) | grep FATAL

If you want both stderr alone, you can use:

( application_to_run 2>&1 >/dev/null ) | grep FATAL

The first sends all output destined for file handle 2 (stderr) to file handle 1 (stdout), then pipes that through grep. The second does the same but also sends stdout to the bit bucket. This will work since redirection is a positional thing. First, stderr is redirected to the current stdout, then stdout is redirected to /dev/null.

生来就爱笑 2024-09-12 13:50:16

如果您询问如何将 stderr 重定向到 stdout 以便可以在管道中使用它,我知道有两种方法:

$ command 2>&1 | ...

$ command |& ..

If you are asking how to redirect stderr to stdout so you can use it in a pipe, there are two ways I know of:

$ command 2>&1 | ...

$ command |& ..
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文