How can log4perl write to STDERR and a file at the same time?

房东的猫 提交于 2021-02-07 09:01:14

问题


I tried to set up two appenders, but it seems to only write to 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);

回答1:


It's not a good idea to mix easy_init with custom appenders. You can fix this by adding appender to logger $logger->add_appender($appender). But this will result in duplicated messages, because you've already set appender in easy_init.

I suggest you to use configuration instead. You can embed configuration in you code:

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");



回答2:


The Log::Log4Perl documentation has the answer half-way through the 'Stealth Logger' section: 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' },
                        );

I had the same question... even 5 years after this was posted, this is still on the front page of Google when searching "perl log4perl write to file and screen"



来源:https://stackoverflow.com/questions/7355141/how-can-log4perl-write-to-stderr-and-a-file-at-the-same-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!