I have a perl script that uses log4perl to log all output to screen and file. Problem is, certain output does not get directed to log4perl.
Script -
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 ($@) {
$logp->error( 'error: ', $@ );
}
$logp->info('stop');
Log4perl config -
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
Here is the output on screen/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
Here is the output in log file -
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
I know i am using print statements instead of log->info, but i would like the log4perl config to be such that all output of the script, irrespective of how it is generated, is directed to log4perl.
any ideas on how i can do this ?
I used 'IPC::Run3' to capture output of any commands/functions that couldn't be captured by log->info/error. I then printed this captured output to log->info/error.
Just to clarify, what i was trying to do above is to capture output of commands executed within the perl code, and redirect that output to log.