log4perl 如何同时写入 STDERR 和文件?

发布于 2024-12-03 10:15:19 字数 615 浏览 1 评论 0原文

我尝试设置两个附加程序,但它似乎只写入 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);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷你仙 2024-12-10 10:15:19

easy_init 与自定义附加程序混合使用并不是一个好主意。您可以通过向记录器添加附加程序$logger->add_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");

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");
美煞众生 2024-12-10 10:15:19

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 年后这个已经发布了,当搜索“perl log4perl write to file and screen”时,它仍然出现在 Google 的首页上

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文