How can I report line number using Log4perl and Moose?

Is it possible for Log4perl to correctly display the line number and the package / log class, and not always. Method :: Delegation on line 99 when used with Moose?

In my case, I created the isa Log :: Log4perl :: Logger attribute and delegated various levels of logging to my class (log, warning, error, ...). Doing this also shows Delegation.pm as a file.

Thank!

+3
source share
2 answers

You did not provide enough information to diagnose your problem (for example, what the :: Delegation method is and how it relates to Log4perl), but my common sense tells me that you probably have a wrapper method from which you call Log4perl methods. You must increase the value $Log::Log4perl::caller_depthinside this shell (and decrease it after calling in Log4perl) to determine the correct location.

eg. in Moose , I use:

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;

Note that the CPAN module MooseX :: Log :: Log4perl does not increment the caller_depth value, which definitely should be.

+2
source

. Log::Log4perl::Layout::PatternLayout %F %L. %L - , , %F - ", ".

%L, :

, .

0

Source: https://habr.com/ru/post/1732080/


All Articles