2

我正在Log4Perlperl.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()?

4

2 回答 2

0

如上所述添加附加程序应该可以修复错误。

附加:
简单地摆脱这个警告的另一个选择是抑制'Log4perl:似乎没有发生初始化。忘记调用 init()?',通过将qw(get_logger :nowarn)添加到 Log4perl 使用语句:

use Log::Log4perl qw(get_logger :nowarn);

您还可以使用Log::Log4perl::initialized()运行初始化检查。
IE

print "Logger initialized\n" if ( Log::Log4perl::initialized() );

更多细节可以在这里找到(Log4perl FAQ):http ://rpm.pbone.net/index.php3/stat/45/idpl/25575098/numer/3/nazwa/Log::Log4perl::FAQ

根据这个文档,警告只出现一次,提醒用户不要忘记在使用之前初始化日志系统。

不过,应该推荐第一个答案。

于 2014-07-18T14:56:23.607 回答
0

您还没有提到附加程序:

my $food_logger = get_logger("Groceries::Food");
$food_logger->level($INFO);

# Appenders
my $appender = Log::Log4perl::Appender->new(
    "Log::Dispatch::File",
    filename => "test.log",
    mode     => "append",
);

$food_logger->add_appender($appender);
于 2013-06-25T13:48:11.463 回答