我有一個使用log4perl將所有輸出記錄到屏幕和文件的perl腳本。 問題是,某些輸出沒有定向到log4perl。將perl腳本的所有輸出指向文件和屏幕
腳本 -
use strict;
use warnings 'FATAL' => 'all';
use Log::Log4perl;
use File::Find;
my $log_conf = "log4perl.conf";
Log::Log4perl::init($log_conf);
my $logp = Log::Log4perl->get_logger();
my $dirSource = 'C:\box';
sub doFileList {
$logp->info('doing dir compare');
find({ wanted => \&process_file, no_chdir => 1 }, $dirSource);
}
sub process_file {
if (-f $_) {
print "This is a file: $_\n";
} else {
print "This is not file: $_\n";
}
}
#main
$logp->info('start');
eval { doFileList(); };
if ([email protected]) {
$logp->error('error: ', [email protected]);
}
$logp->info('stop');
Log4perl配置 -
log4perl.rootLogger = DEBUG, screen, file
log4perl.appender.screen = Log::Log4perl::Appender::Screen
log4perl.appender.screen.stderr = 0
log4perl.appender.screen.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.screen.layout.ConversionPattern = %d [%M:%L] %p %F{2} - %m%n
log4perl.appender.file = Log::Log4perl::Appender::File
log4perl.appender.file.filename = log/compare.log
log4perl.appender.file.mode = append
log4perl.appender.file.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.file.layout.ConversionPattern = %d [%M:%L] %p %F{2} - %m%n
這是在屏幕上輸出/ IDE -
2013/03/04 11:56:28 [main:::29] INFO DirCompare\DirCompare.pl - start
2013/03/04 11:56:28 [tools::dircompare::doFileList:16] INFO DirCompare\DirCompare.pl - doing dir compare
This is not file: C:\box
This is a file: C:\box\test01.JPG
This is a file: C:\box\test02.JPG
2013/03/04 11:56:28 [main:::34] INFO DirCompare\DirCompare.pl - stop
下面是日誌文件的輸出 -
2013/03/04 11:56:28 [main:::29] INFO DirCompare\DirCompare.pl - start
2013/03/04 11:56:28 [tools::dircompare::doFileList:16] INFO DirCompare\DirCompare.pl - doing dir compare
2013/03/04 11:56:28 [main:::34] INFO DirCompare\DirCompare.pl - stop
我知道我使用的是打印語句而不是log-> info,但我希望log4perl的配置是這樣的,腳本的所有輸出,不管它是如何生成的,都直接指向log4perl。
關於我如何做到這一點的任何想法?