我可以将 STDOUT 和 STDERR 发送到日志文件并发送到 Win32 Perl 中的屏幕吗?

发布于 2024-08-07 21:35:21 字数 509 浏览 8 评论 0原文

我在互联网上搜索并找到了一些将 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 技术交流群。

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

发布评论

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

评论(8

凉栀 2024-08-14 21:35:21

我使用 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.

爱你是孤单的心事 2024-08-14 21:35:21
use PerlIO::Util;
*STDOUT->push_layer(tee => ">>/dir/dir/file");
*STDERR->push_layer(tee => ">>/dir/dir/file");

虽然我广泛使用 Log::Dispatch ,但我使用上面的内容来记录什么实际上是显示到屏幕上的一个文件。

use PerlIO::Util;
*STDOUT->push_layer(tee => ">>/dir/dir/file");
*STDERR->push_layer(tee => ">>/dir/dir/file");

Though I use Log::Dispatch extensively, I've used the above to log what actually got displayed to the screen to a file.

关于从前 2024-08-14 21:35:21

您可以在 Windows shell 级别将 stderr 重定向到 stdout,方法如下:

perl stuff.pl 2>&1

请参阅 这里的支持文章为官方说法。

然后你可以使用 这个 stackoverflow 答案 从 shell 执行 tee

perl stuff.pl 2>&1 | tee stuff.txt

You can redirect stderr to stdout at the windows shell level by doing something like:

perl stuff.pl 2>&1

See support article here for the official word.

Then you could use this stackoverflow answer to do a tee from the shell.

perl stuff.pl 2>&1 | tee stuff.txt
亚希 2024-08-14 21:35:21

只需重新分配 STDERR 文件句柄...

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 );
*STDERR = *$tee{IO};
select $tee;

应该提到的是,我在 Windows 上对此进行了测试,它有效,但我使用的是 StrawberryPerl。

Simply reassign the STDERR filehandle ...

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 );
*STDERR = *$tee{IO};
select $tee;

Should mention that I tested this on Windows, it works, however I use StrawberryPerl.

护你周全 2024-08-14 21:35:21

我没有 Windows 盒子来测试这个,但也许你可以做一些事情,比如制作一个绑定句柄,它将打印到 STDOUT 和日志,然后将 STDOUT 和 STDERR 重定向到它?

编辑:我唯一担心的是存储 STDOUT 供以后使用的方法,如果第一个在 Windows 上不起作用,我添加了第二种存储 STDOUT 供以后使用的可能性。他们都在 Linux 上为我工作。

#!/usr/bin/perl

use strict;
use warnings;

tie *NEWOUT, 'MyHandle', 'test.log';
*STDOUT = *NEWOUT;
*STDERR = *NEWOUT;

print "Print\n";
warn "Warn\n";

package MyHandle;

sub TIEHANDLE {
  my $class = shift;
  my $filename = shift;

  open my $fh, '>', $filename or die "Could not open file $filename";

  ## Use one of these next two lines to store STDOUT for later use.
  ## Both work for me on Linux, if one does not work on Windows try the other.
  open(OLDSTDOUT, '>&STDOUT') or die "Could not store STDOUT";
  #*OLDSTDOUT = *STDOUT;

  my $self = {
    loghandle => $fh,
    logfilename => $filename,
    stdout => \*OLDSTDOUT,
  };

  bless $self, $class;

  return $self;
}

sub PRINT {
  my $self = shift;
  my $log = $self->{loghandle};
  my $stdout = $self->{stdout};
  print $log @_;
  print $stdout @_;
}

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.

#!/usr/bin/perl

use strict;
use warnings;

tie *NEWOUT, 'MyHandle', 'test.log';
*STDOUT = *NEWOUT;
*STDERR = *NEWOUT;

print "Print\n";
warn "Warn\n";

package MyHandle;

sub TIEHANDLE {
  my $class = shift;
  my $filename = shift;

  open my $fh, '>', $filename or die "Could not open file $filename";

  ## Use one of these next two lines to store STDOUT for later use.
  ## Both work for me on Linux, if one does not work on Windows try the other.
  open(OLDSTDOUT, '>&STDOUT') or die "Could not store STDOUT";
  #*OLDSTDOUT = *STDOUT;

  my $self = {
    loghandle => $fh,
    logfilename => $filename,
    stdout => \*OLDSTDOUT,
  };

  bless $self, $class;

  return $self;
}

sub PRINT {
  my $self = shift;
  my $log = $self->{loghandle};
  my $stdout = $self->{stdout};
  print $log @_;
  print $stdout @_;
}
⊕婉儿 2024-08-14 21:35:21

try :

my logfh;
my $logfn = "some/path/to/file.log";
open ($logfh, '>',$logfn ) or die "Error opening logfile $logfn\n";
my $tee = IO::Tee->new( $logfh);
my $tee2 = IO::Tee->new( $logfh, \*STDOUT );
# all naked print statements will send output to log file
select($tee);
# all STDERR print statements will send output to console
*STDERR = *$tee2{IO};

所有不指定文件句柄的打印语句(即任何常规消息)都会将输出发送到日志文件。所有使用 STDERR 文件句柄的打印语句(即所有错误)都会将输出发送到控制台和日志文件。

try :

my logfh;
my $logfn = "some/path/to/file.log";
open ($logfh, '>',$logfn ) or die "Error opening logfile $logfn\n";
my $tee = IO::Tee->new( $logfh);
my $tee2 = IO::Tee->new( $logfh, \*STDOUT );
# all naked print statements will send output to log file
select($tee);
# all STDERR print statements will send output to console
*STDERR = *$tee2{IO};

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.

寄与心 2024-08-14 21:35:21

那么您希望 STDERR 表现得像 STDOUT 一样,同时显示屏幕和同一个日志文件吗?您可以直接复制 STDERR

open(STDERR, ">&STDOUT") or warn "failed to dup STDOUT:$!";

(我不知道您是否会在调用 IO::tee->new 之前或之后执行此操作)。

So you want STDERR to behave like STDOUT, going to both the screen and the same log file? Can you just dup STDERR with

open(STDERR, ">&STDOUT") or warn "failed to dup STDOUT:$!";

(I don't know offhand whether you would do this before or after the call to IO::tee->new).

俯瞰星空 2024-08-14 21:35:21

我编写了一个带有可配置动态日志记录的简约 perl 记录器,为您提供以下 API:

        use strict ; use warnings ; use Exporter;
        use Configurator ; 
        use Logger ; 


        #   anonymous hash !!!
        our $confHolder = () ; 

        sub main {

                # strip the remote path and keep the bare name
                $0=~m/^(.*)(\\|\/)(.*)\.([a-z]*)/; 
                my $MyBareName = $3; 
                my $RunDir= $1 ; 

                # create the configurator object 
                my $objConfigurator = new Configurator($RunDir , $MyBareName ); 
                # get the hash having the vars 
                $confHolder = $objConfigurator ->getConfHolder () ; 
                # pring the hash vars 
                print $objConfigurator->dumpIni();  

                my $objLogger = new Logger (\$confHolder) ; 
                $objLogger->LogMsg  (   " START MAIN " ) ;  

                $objLogger->LogMsg  (   "my \$RunDir is $RunDir" ) ; 
                $objLogger->LogMsg  (   "this is a simple message" ) ; 
                $objLogger->LogErrorMsg (   "This is an error message " ) ; 
                $objLogger->LogWarningMsg   (   "This is a warning message " ) ; 
                $objLogger->LogInfoMsg  (   "This is a info message " ) ; 
                $objLogger->LogDebugMsg (   "This is a debug message " ) ; 
                $objLogger->LogTraceMsg (   "This is a trace message " ) ; 
                $objLogger->LogMsg  (   "using the following log file " .  "$confHolder->{'LogFile'}" ) ; 
                $objLogger->LogMsg  (   " STOP MAIN \n\n" ) ; 

        } #eof main 



        #Action !!!
        main(); 

        1 ; 

        __END__

I wrote a minimalistic perl logger with configurable dynamic logging giving you the following API:

        use strict ; use warnings ; use Exporter;
        use Configurator ; 
        use Logger ; 


        #   anonymous hash !!!
        our $confHolder = () ; 

        sub main {

                # strip the remote path and keep the bare name
                $0=~m/^(.*)(\\|\/)(.*)\.([a-z]*)/; 
                my $MyBareName = $3; 
                my $RunDir= $1 ; 

                # create the configurator object 
                my $objConfigurator = new Configurator($RunDir , $MyBareName ); 
                # get the hash having the vars 
                $confHolder = $objConfigurator ->getConfHolder () ; 
                # pring the hash vars 
                print $objConfigurator->dumpIni();  

                my $objLogger = new Logger (\$confHolder) ; 
                $objLogger->LogMsg  (   " START MAIN " ) ;  

                $objLogger->LogMsg  (   "my \$RunDir is $RunDir" ) ; 
                $objLogger->LogMsg  (   "this is a simple message" ) ; 
                $objLogger->LogErrorMsg (   "This is an error message " ) ; 
                $objLogger->LogWarningMsg   (   "This is a warning message " ) ; 
                $objLogger->LogInfoMsg  (   "This is a info message " ) ; 
                $objLogger->LogDebugMsg (   "This is a debug message " ) ; 
                $objLogger->LogTraceMsg (   "This is a trace message " ) ; 
                $objLogger->LogMsg  (   "using the following log file " .  "$confHolder->{'LogFile'}" ) ; 
                $objLogger->LogMsg  (   " STOP MAIN \n\n" ) ; 

        } #eof main 



        #Action !!!
        main(); 

        1 ; 

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