如何将 Perl 警告捕获到 Log4perl 日志中?
Log4perl 是一个很棒的日志记录工具。
警告编译指示也是一个重要的工具。
然而,当 Perl 脚本作为守护进程运行时,Perl 警告将打印到 STDERR 中,而没有人可以看到它们,而不是打印到相关程序的 Log4perl 日志文件中。
有没有办法将 Perl 警告捕获到 Log4perl 日志中?
例如,此代码将很好地记录到日志文件中,但如果它作为守护程序运行,Perl 警告将不会包含在日志中:
#!/usr/bin/env perl
use strict;
use warnings;
use Log::Log4perl qw(get_logger);
# Define configuration
my $conf = q(
log4perl.logger = DEBUG, FileApp
log4perl.appender.FileApp = Log::Log4perl::Appender::File
log4perl.appender.FileApp.filename = test.log
log4perl.appender.FileApp.layout = PatternLayout
);
# Initialize logging behaviour
Log::Log4perl->init( \$conf );
# Obtain a logger instance
my $logger = get_logger("Foo::Bar");
$logger->error("Oh my, an error!");
$SIG{__WARN__} = sub {
#local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
$logger->warn("WARN @_");
};
my $foo = 100;
my $foo = 44;
这仍然打印到 STDERR:
"my" variable $foo masks earlier declaration in same scope at log.pl line 27.
并且日志文件不会捕获这个警告。
Log4perl is a great tool for logging.
The warnings pragma is also an essential tool.
However, when Perl scripts are running as daemons, the Perl warnings, are printed into STDERR where nobody can see them, and not into the Log4perl log file of the relevant program.
Is there a way to catch Perl warnings into the Log4perl log?
For example, this code will log just fine to the log file, but in case this is run as a daemon, the Perl warnings will be not be included in the log:
#!/usr/bin/env perl
use strict;
use warnings;
use Log::Log4perl qw(get_logger);
# Define configuration
my $conf = q(
log4perl.logger = DEBUG, FileApp
log4perl.appender.FileApp = Log::Log4perl::Appender::File
log4perl.appender.FileApp.filename = test.log
log4perl.appender.FileApp.layout = PatternLayout
);
# Initialize logging behaviour
Log::Log4perl->init( \$conf );
# Obtain a logger instance
my $logger = get_logger("Foo::Bar");
$logger->error("Oh my, an error!");
$SIG{__WARN__} = sub {
#local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
$logger->warn("WARN @_");
};
my $foo = 100;
my $foo = 44;
This still prints out to STDERR:
"my" variable $foo masks earlier declaration in same scope at log.pl line 27.
And the log file does not catch this warning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以安装一个 WARN 处理程序来执行此操作。 Log4perl 常见问题解答。
我的程序已经使用 warn() 和 die()。我如何切换到 Log4perl?
如果您的程序已经使用 Perl 的 warn() 函数来输出错误消息,并且您希望将这些消息引入 Log4perl 世界,只需定义一个 WARN程序或模块所在的处理程序:
这将捕获程序中任何显式使用
warn
的情况,以及有关使用未初始化值等的 Perlish 警告。You could install a
WARN
handler to do this. It's mentioned in the Log4perl FAQ.My program already uses warn() and die(). How can I switch to Log4perl?
If your program already uses Perl's warn() function to spew out error messages and you'd like to channel those into the Log4perl world, just define a WARN handler where your program or module resides:
This will capture any explicit use of
warn
in your program, as well as Perlish warnings about use of uninitialized values and the like.它在 Log4perl FAQ 中为 一些模块打印发送至 STDERR 的消息。如何将它们汇集到 Log::Log4perl? 和 我的程序已经使用 warn() 和 die()。如何切换到 Log4perl?。
您的具体问题有一个额外的问题,您会看到编译时警告,因此您需要调整常见问题解答建议以尽早在编译时设置日志记录。在尽可能靠近源代码顶部的
BEGIN
块中执行此操作:您可以通过在启用警告的情况下运行语法检查来判断这是否是编译时警告:
如果您看到警告在语法检查期间,这是一个编译时警告。
不过,我宁愿在开发中捕获编译时警告。他们不应该进入生产系统。 :)
It's in the Log4perl FAQ as Some module prints messages to STDERR. How can I funnel them to Log::Log4perl? and My program already uses warn() and die(). How can I switch to Log4perl?.
Your specific problem has the added wrinkle that you are seeing a compile-time warning, so you need to adjust the FAQ advice to setup the logging at compile time as early as possible. Do that in a
BEGIN
block as close to the top of the source as you can stand:You can tell if it's a compile-time warning by running a syntax check with warnings enabled:
If you see the warning during the syntax check, it's a compile-time warning.
I'd rather catch the compile-time warnings in development though. They shouldn't make it through to a production system. :)