如何判断 Log4perl 在运行期间是否发出任何警告?

发布于 2024-08-27 11:20:34 字数 450 浏览 8 评论 0原文

我一直在许多脚本中广泛使用 Log4perl 。我想扩充这些脚本,以便在记录了任何 WARNERROR 消息时设置错误代码。根据现有文档,我找不到任何明显的方法来做到这一点。

我想避免对现有脚本进行强力重写,以添加对每个 WARNERROR 日志消息的检查;如果可能的话,我更愿意在脚本退出之前处理它,就像这样的伪代码:

if $log->has_warnings_or_errors then
  exit 1
else
  exit 0

有没有简单的方法来调用 Log4Perl 来确定当前记录器是否有某些级别的问题消息?

I've been using Log4perl extensively in a number of scripts. I'd like to augment those scripts to set an error code if any WARN or ERROR messages have been logged. I couldn't find any obvious way to do this based on existing documentation.

I'd like to avoid a brute-force rewrite of my existing scripts to add a check on every WARN or ERROR log message; I'd prefer to handle it prior to script exit if possible like this pseudocode:

if $log->has_warnings_or_errors then
  exit 1
else
  exit 0

Is there any easy way to call Log4Perl to determine if the current logger has issues messages of certain levels?

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

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

发布评论

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

评论(2

浅暮の光 2024-09-03 11:20:34

使用 appender

MyCounter.pm:

package MyCounter;

use warnings;
use strict;

use Log::Log4perl::Level;

sub new {
  my($class,%arg) = @_;
  bless {} => $class;
}

sub log {
  my($self,%arg) = @_;
  ++$self->{ $arg{log4p_level} };
}

sub howmany {
  my($self,@which) = @_;
  my $total = 0;
  $total += ($self->{$_} || 0) for @which;
  $total;
}

1;

myprog:

#! /usr/bin/perl

use warnings;
use strict;

use Log::Log4perl;

my $conf = q(
  log4perl.category.MyLogger = INFO, Screen

  log4perl.appender.Screen = Log::Log4perl::Appender::Screen
  log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout
);

Log::Log4perl->init(\$conf);

my $l = Log::Log4perl->get_logger("MyLogger");

my $counter = Log::Log4perl::Appender->new("MyCounter");
$l->add_appender($counter);

$l->warn("warning");
$l->info("info");
$l->error("incorrect");
$l->fatal("really bad, man");

print $counter->howmany(qw/ WARN ERROR FATAL /), "\n";

exit ($counter->howmany(qw/ WARN ERROR FATAL /) ? 1 : 0);

输出:

$ ./myprog 
WARN - warning
INFO - info
ERROR - incorrect
FATAL - really bad, man
3
$ echo $?
1

注释掉 ...->warn...->error...->fatal 行来获取

$ ./myprog 
INFO - info
0
$ echo $?
0

Use an appender.

MyCounter.pm:

package MyCounter;

use warnings;
use strict;

use Log::Log4perl::Level;

sub new {
  my($class,%arg) = @_;
  bless {} => $class;
}

sub log {
  my($self,%arg) = @_;
  ++$self->{ $arg{log4p_level} };
}

sub howmany {
  my($self,@which) = @_;
  my $total = 0;
  $total += ($self->{$_} || 0) for @which;
  $total;
}

1;

myprog:

#! /usr/bin/perl

use warnings;
use strict;

use Log::Log4perl;

my $conf = q(
  log4perl.category.MyLogger = INFO, Screen

  log4perl.appender.Screen = Log::Log4perl::Appender::Screen
  log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout
);

Log::Log4perl->init(\$conf);

my $l = Log::Log4perl->get_logger("MyLogger");

my $counter = Log::Log4perl::Appender->new("MyCounter");
$l->add_appender($counter);

$l->warn("warning");
$l->info("info");
$l->error("incorrect");
$l->fatal("really bad, man");

print $counter->howmany(qw/ WARN ERROR FATAL /), "\n";

exit ($counter->howmany(qw/ WARN ERROR FATAL /) ? 1 : 0);

Output:

$ ./myprog 
WARN - warning
INFO - info
ERROR - incorrect
FATAL - really bad, man
3
$ echo $?
1

Comment out the ...->warn, ...->error, and ...->fatal lines to get

$ ./myprog 
INFO - info
0
$ echo $?
0
陌伤浅笑 2024-09-03 11:20:34

您可以使用 包装器 并编写自定义 fatalerrorwarn 方法,用于递增计数器以及返回该计数器值的访问器。

You could use a wrapper and write custom fatal, error and warn methods that increment a counter and an accessor that returns the value of that counter.

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