I am reading about Log4Perl
from perl.com
I am trying to follow the tutorial but got "lost" in the part Logger Categories
.
I tried to follow the tutorial and did the following scripts:
I created a directory Groceries
and inside I created a pm
file as follows:
Food.pm:
#!/usr/bin/perl
use strict;
use warnings;
package Groceries::Food;
use Log::Log4perl qw(get_logger);
sub new {
my($class, $what) = @_;
my $logger = get_logger("Groceries::Food");
if(defined $what) {
$logger->debug("New food: $what");
return bless { what => $what }, $class;
}
$logger->error("No food defined");
return undef;
}
sub consume {
my($self) = @_;
my $logger = get_logger("Groceries::Food");
$logger->info("Eating $self->{what}");
}
This is copy/paste from perl.com mentioned above.
Then in the same directory that Groceries
directories is in (i.e. not in the same directory that Food.pm
resides) I created another script as follows:
test_script.pl:
#!/usr/bin/perl
use strict;
use warnings;
######### System initialization section ###
use Log::Log4perl qw(get_logger :levels);
my $food_logger = get_logger("Groceries");
$food_logger->level($INFO);
sub test_log(){
my $what = @_;
if(defined $what){
$food_logger->info("Defined:", $what);
}
else{
$food_logger->error("Nothing defined");
}
}
test_log();
But when I run the test script it does not work. I get:
Log4perl: Seems like no initialization happened. Forgot to call init()?
Also if I add:
test_log("something");
I get:
Too many arguments for main::test_log at testlogger.pl line 23, near ""something")"
Execution of testlogger.pl aborted due to compilation errors.
What am I doing wrong here?
UPDATE
I followed the tutorial and modified the way it says at listing 2: eat.pl
#!/usr/bin/perl
use strict;
use warnings;
use Log::Log4perl qw(get_logger :levels);
######### System initialization section ###
my $food_logger = get_logger("Groceries::Food");
$food_logger->level($INFO);
#############Run it ###
my $o = Groceries::Food->new("Chicken");
$o->consume();
####Application section ####
package Groceries::Food;
use Log::Log4perl qw(get_logger);
sub new {
my($class, $what) = @_;
my $logger = $food_logger;#get_logger("Groceries::Food");
if(defined $what) {
$logger->debug("New food: $what");
return bless { what => $what }, $class;
}
$logger->error("No food defined");
return undef;
}
sub consume {
my($self) = @_;
my $logger = $food_logger;#get_logger("Groceries::Food");
$logger->info("Eating $self->{what}");
}
I still get Log4perl: Seems like no initialization happened. Forgot to call init()?