我正在Log4Perl
从perl.com
阅读有关内容,
我正在尝试按照教程进行操作,但在该部分中“迷路了” Logger Categories
。
我尝试按照教程进行操作并执行了以下脚本:
我创建了一个目录Groceries
,并在其中创建了一个pm
文件,如下所示:
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}");
}
这是上面提到的 perl.com 的复制/粘贴。
然后在目录所在的同一目录Groceries
中(即不在Food.pm
驻留的同一目录中)我创建了另一个脚本,如下所示:
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();
但是当我运行测试脚本时它不起作用。我得到:
Log4perl: Seems like no initialization happened. Forgot to call init()?
另外,如果我添加:
test_log("something");
我得到:
Too many arguments for main::test_log at testlogger.pl line 23, near ""something")"
Execution of testlogger.pl aborted due to compilation errors.
我在这里做错了什么?
更新
我按照教程修改了它在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}");
}
我仍然得到Log4perl: Seems like no initialization happened. Forgot to call init()?