我可以将 STDOUT 和 STDERR 发送到日志文件并发送到 Win32 Perl 中的屏幕吗?
我在互联网上搜索并找到了一些将 STDOUT 发送到 2 个不同位置的好解决方案。就像日志文件一样,同时也像屏幕一样。这是一个例子:
use IO::Tee;
my $log_filename = "log.txt";
my $log_filehandle;
open( $log_filehandle, '>>', $log_filename )
or die("Can't open $log_filename for append: $!");
my $tee = IO::Tee->new( $log_filehandle, \*STDOUT );
select $tee;
但是这个解决方案使 STDERR 只显示在屏幕上,而我希望 STDERR 既显示在屏幕上,也显示在 STDOUT 所记录到的同一日志文件中。这可能吗?
我的任务是记录我的构建过程,但我也想像往常一样在 IDE 的屏幕上看到它。记录错误消息与记录快乐消息同样重要。将错误记录到单独的日志文件中并不是一个好的解决方案。
I've searched the Internet and have found some good solutions for teeing STDOUT to 2 different places. Like to a log file and also to the screen at the same time. Here's one example:
use IO::Tee;
my $log_filename = "log.txt";
my $log_filehandle;
open( $log_filehandle, '>>', $log_filename )
or die("Can't open $log_filename for append: $!");
my $tee = IO::Tee->new( $log_filehandle, \*STDOUT );
select $tee;
But this solution leaves STDERR going only to the screen and I want STDERR go to both the screen and also to the same log file that STDOUT is being logged to. Is that even possible?
My task is to get my build process logged, but I also want to see it on my IDE's screen as usual. And logging the error messages is just as important as logging the happy messages. And getting the errors logged to a separate log file is not a good solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我使用 Log::Log4perl 来处理类似的事情。它为您处理将输出发送到多个位置,包括屏幕、文件、数据库或您喜欢的任何其他位置。一旦你变得有点复杂,你就不应该自己做这些事情。
您只需向 Log4perl 发送一条消息,它就会计算出其余的内容,而不是打印到文件句柄。我在 掌握 Perl 中有一个简短的介绍。它基于 Log4j,并且您可以在 Log4j 中完成的大部分操作也可以在 Log4perl 中完成,这也意味着一旦您了解它,它就成为一项可转移的技能。
I use Log::Log4perl for things like this. It handles sending output to multiple places for you, including the screen, files, databases, or whatever else you like. Once you get even a little bit complex, you shouldn't be doing this stuff on your own.
Instead of printing to filehandles, you just give Log4perl a message and it figures out the rest. I have a short introduction to it in Mastering Perl. It's based on Log4j, and most of the stuff you can do in Log4j you can do in Log4perl, which also means that once you know it, it becomes a transferrable skill.
虽然我广泛使用 Log::Dispatch ,但我使用上面的内容来记录什么实际上是显示到屏幕上的一个文件。
Though I use Log::Dispatch extensively, I've used the above to log what actually got displayed to the screen to a file.
您可以在 Windows shell 级别将
stderr
重定向到stdout
,方法如下:请参阅 这里的支持文章为官方说法。
然后你可以使用 这个 stackoverflow 答案 从 shell 执行
tee
。You can redirect
stderr
tostdout
at the windows shell level by doing something like:See support article here for the official word.
Then you could use this stackoverflow answer to do a
tee
from the shell.只需重新分配 STDERR 文件句柄...
应该提到的是,我在 Windows 上对此进行了测试,它有效,但我使用的是 StrawberryPerl。
Simply reassign the
STDERR
filehandle ...Should mention that I tested this on Windows, it works, however I use StrawberryPerl.
我没有 Windows 盒子来测试这个,但也许你可以做一些事情,比如制作一个绑定句柄,它将打印到 STDOUT 和日志,然后将 STDOUT 和 STDERR 重定向到它?
编辑:我唯一担心的是存储 STDOUT 供以后使用的方法,如果第一个在 Windows 上不起作用,我添加了第二种存储 STDOUT 供以后使用的可能性。他们都在 Linux 上为我工作。
I don't have a windows box to test this on, but perhaps you could do something like making a tied handle which will print to both STDOUT and a log, then redirecting STDOUT and STDERR to it?
EDIT: The only fear I have is the method of storing STDOUT for later use, I have added a second possibility for storing STDOUT for later use should the first not work on Windows. They both work for me on Linux.
try :
所有不指定文件句柄的打印语句(即任何常规消息)都会将输出发送到日志文件。所有使用 STDERR 文件句柄的打印语句(即所有错误)都会将输出发送到控制台和日志文件。
try :
All print statements that do not specify a file handle (that is, any regular messages) will send output to the log file. All print statements that use the STDERR file handle (that is, all errors) will send output to both the console and the log file.
那么您希望
STDERR
表现得像STDOUT
一样,同时显示屏幕和同一个日志文件吗?您可以直接复制STDERR
吗(我不知道您是否会在调用
IO::tee->new
之前或之后执行此操作)。So you want
STDERR
to behave likeSTDOUT
, going to both the screen and the same log file? Can you just dupSTDERR
with(I don't know offhand whether you would do this before or after the call to
IO::tee->new
).我编写了一个带有可配置动态日志记录的简约 perl 记录器,为您提供以下 API:
I wrote a minimalistic perl logger with configurable dynamic logging giving you the following API: