1

这是我的代码:

use strict;
use warnings;
use Log::Log4perl qw(:easy);

Log::Log4perl->init({
 level => $DEBUG
});

my $logger = Log::Log4perl->get_logger("my.logger");
my $appender = Log::Log4perl::Appender->new("Log::Log4perl::Appender::File",filename => "my.file");
$appender->layout(Log::Log4perl::Layout::SimpleLayout->new);
$logger->add_appender($appender);

$logger->info("this is an info");

我要做的就是将消息记录到文件中,并显示级别。我明白这就是SimpleLayout它的用途。我想在没有配置文件的情况下执行此操作。运行上面的代码会显示以下消息:

Log::Log4perl configuration looks suspicious: No loggers defined

4

1 回答 1

2

From http://log4perl.sourceforge.net/releases/Log-Log4perl/docs/html/Log/Log4perl/FAQ.html#f625e , try this approach:

use Log::Log4perl qw(:easy);
# Append to a log file
Log::Log4perl->easy_init( { level   => $DEBUG,
                            file    => ">>my.file" } );
于 2010-05-22T19:05:38.817 回答