我尝试设置两个appender,但似乎只写STDERR:
my $header = "######$scriptname $version";
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init($DEBUG);
my $logger = get_logger();
my $layout = Log::Log4perl::Layout::PatternLayout->new(
"%d %p> %F{1}:%L %M - %m%n");
my $appender = Log::Log4perl::Appender->new(
"Log::Dispatch::File",
filename=>$scriptname.".log",
mode => "append"
);
$appender->layout($layout);
my $stderr = Log::Log4perl::Appender::Screen->new(
stderr =>0,
);
$stderr->layout($layout);
$logger->info($header);
答案 0 :(得分:5)
将easy_init
与自定义附加程序混合使用并不是一个好主意。您可以通过向记录器$logger->add_appender($appender)
添加appender来解决此问题。但这会导致重复的消息,因为您已经在easy_init
中设置了appender。
我建议你改用配置。您可以在代码中嵌入配置:
use strict; use warnings;
use Log::Log4perl;
Log::Log4perl->init(\<<CONFIG);
log4perl.rootLogger = DEBUG, screen, file
log4perl.appender.screen = Log::Log4perl::Appender::Screen
log4perl.appender.screen.stderr = 0
log4perl.appender.screen.layout = PatternLayout
log4perl.appender.screen.layout.ConversionPattern = %d %p> %F{1}:%L %M - %m%n
log4perl.appender.file = Log::Log4perl::Appender::File
log4perl.appender.file.filename = 1.log
log4perl.appender.file.mode = append
log4perl.appender.file.layout = PatternLayout
log4perl.appender.file.layout.ConversionPattern = %d %p> %F{1}:%L %M - %m%n
CONFIG
my $logger = Log::Log4perl->get_logger();
$logger->info("my message");
答案 1 :(得分:4)
Log :: Log4Perl文档在“Stealth Logger”的中途得到了答案。部分: http://search.cpan.org/~mschilli/Log-Log4perl-1.49/lib/Log/Log4perl.pm#Stealth_loggers
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init( { level => $DEBUG,
file => ">>test.log",
layout => '%F{1}-%L-%M: %m%n' },
{ level => $DEBUG,
file => "STDOUT",
layout => '%m%n' },
);
我有同样的问题...即使在发布这个问题的5年后,这仍然是Google搜索时的首页&#34; perl log4perl写入文件和屏幕&#34;