当与Moose一起使用时,是否可以让Log4perl正确显示日志事件的行号和包/类,而不是始终在第99行显示Method::Delegation?
在我的例子中,我创建了一个属性isa Log::Log4perl::Logger,并将各种日志级别委托给我的类(log、warn、error等)。这样做还会将Delegation.pm显示为文件。
谢谢!
发布于 2010-02-10 04:59:27
您没有提供足够的信息来诊断您的问题(例如,什么是Method::Delegation以及它是如何连接到Log4perl的),但是我敏锐的感觉告诉我,您可能有一个包装器方法,您可以从中调用Log4perl方法。您应该在此包装器中增加$Log::Log4perl::caller_depth的值(并在调用Log4perl后将其减少),以便确定正确的位置。
例如,在Moose中,我使用:
package MyApp::Role::Log;
use MooseX::Role::Strict;
use Log::Log4perl;
my @methods = qw(
log trace debug info warn error fatal
is_trace is_debug is_info is_warn is_error is_fatal
logexit logwarn error_warn logdie error_die
logcarp logcluck logcroak logconfess
);
has '_logger' => (
is => 'ro',
isa => 'Log::Log4perl::Logger',
lazy_build => 1,
handles => \@methods,
);
around $_ => sub {
my $orig = shift;
my $this = shift;
# one level for this method itself
# two levels for Class::MOP::Method::Wrapped (the "around" wrapper)
# one level for Moose::Meta::Method::Delegation (the "handles" wrapper)
local $Log::Log4perl::caller_depth += 4;
my $return = $this->$orig(@_);
$Log::Log4perl::caller_depth -= 4;
return $return;
} foreach @methods;
sub _build__logger
{
my $this = shift;
Log::Log4perl->easy_init() if not Log::Log4perl::initialized();
return Log::Log4perl->get_logger(ref $this)
}
no MooseX::Role::Strict;
1;请注意,CPAN模块MooseX::Log::Log4perl不会递增caller_depth,这是它绝对应该递增的。
发布于 2010-02-10 08:07:47
是。根据Log::Log4perl::Layout::PatternLayout的说法,你想要的是%F和%L的组合。%L是“发出日志语句的文件中的行号”,%F是“发生日志事件的文件”。
或者,最简单的方法是使用%l,它是:
调用方法的完全限定名称,后跟调用方源、文件名和括号之间的行号。
https://stackoverflow.com/questions/2232430
复制相似问题