I'm able to get the log mechanism working using the module, but i'm not able to get the "DEBUG"
statements working and also facing "message duplication" issues in the log file
ABC::Parser/pm
package ABC::Parser;
use strict;
use warnings;
use File::Slurp 'read_file';
use Data::Dumper;
use Log::Log4perl qw(:easy);
Log::Log4perl::init( "../conf/log4perl.conf" );
my $logger = Log::Log4perl::get_logger("ABC::Parser");
sub get_data($) {
my ($params) = @_;
my $raw_file = $params->{file};
my @raw_data = read_file($raw_file, err_mode => 'carp');
my $data = {};
$logger->info("done parsing");
$logger->warn("error statement");
$logger->debug("DEbug statement");
return $data;
}
1;
test_parser.pl
#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use Log::Log4perl qw(:easy);
use ABC::Parser;
use vars qw($debug);
GetOptions(
"debug|x" => \$debug,
"help|h" => \&usage,
);
##Initializations
Log::Log4perl::init( "../conf/log4perl.conf" );
my $logger = Log::Log4perl::get_logger();
my $data = ABC::Parser::get_data({file => "/tmp/test.data"});
$logger->error("could not get parsed data") if(!$data);
$logger->info("Created output");
$logger->debug("$data") if ($debug);
sub get_log{
use File::Basename;
use POSIX qw(strftime);
my $now_string = strftime("%Y-%m-%d", localtime);
my $log = sprintf "%s.$now_string.info.log", basename( $0, '.pl' )
+;
return $log;
}
__END__
Below is the script output
-bash-3.00$ ./test_parser.pl
2009/07/26 06:13:29 INFO> Starting ./test_parser.pl
2009/07/26 06:13:29 INFO> done parsing
2009/07/26 06:13:29 WARN> error
2009/07/26 06:13:29 INFO> Created output file
Below is the output written to the log file, which has repeated statements
2009/07/26 06:13:29 INFO> Starting ./parse_xheader.pl
2009/07/26 06:13:29 INFO> Starting ./parse_xheader.pl
2009/07/26 06:13:29 INFO> Starting ./parse_xheader.pl
2009/07/26 06:13:29 INFO> done parsing
2009/07/26 06:13:29 INFO> done parsing
2009/07/26 06:13:29 INFO> done parsing
2009/07/26 06:13:29 WARN> error
2009/07/26 06:13:29 WARN> error
2009/07/26 06:13:29 WARN> error
2009/07/26 06:13:29 INFO> Created output file
2009/07/26 06:13:29 INFO> Created output file
2009/07/26 06:13:29 INFO> Created output file
Below are the changes which I want to carry out, please let me know how to achieve this
* Cleanup the log4perl.conf, so that all INFO, ERROR and DEBUG statements go into a single log file
without the "message duplication", which is happening currently.
* How to specify the "logDirectory" path to a perl variable in log4perl.conf, so that this can be easily changed.
* I want to enable "DEBUG" statements, when the user passes "-d" parameter to the perl script (or there own perl scr and also log them to
the log file
* How to enable "DEBUG" statements in "Parser2.pm" by default.
Thanks in advance for your time.