nysus has asked for the wisdom of the Perl Monks concerning the following question:

I have the following:

{ package MyApp; use Moose; use Moose::Util qw( apply_all_roles ); with 'MooseX::Log::Log4perl'; sub BUILD { my $s = shift; # apply_all_roles($s, 'BarCollector'); # if you uncomment this li +ne, logging doesn't work anymore } sub print_something { my $self = shift; $self->log->info('This does not print when apply_all_roles is used +.'); } } { package BarCollector; use Moose::Role # does nothing } #! /usr/bin/env perl use File::HomeDir; use Log::Log4perl; BEGIN { Log::Log4perl->init_once(File::HomeDir->my_home . '/test.cfg') + }; use MyApp; my $app = MyApp->new(); $app->print_something; __END__ # test.cfg config file: log4perl.logger.MyApp=TRACE, LOG log4perl.appender.LOG=Log::Log4perl::Appender::ScreenColoredLevels log4perl.appender.LOG.layout=PatternLayout log4perl.appender.LOG.layout.ConversionPattern=%c - %m%n

As noted in the comment, when the line that contains apply_all_roles is uncommented, log messages no longer work. There are no errors.

Update: One other clue is that when I change the first line of the config file to: log4perl.rootLogger=TRACE, LOG

I get output, though the category looks something like this: Moose.Meta.Class.__ANON__.SERIAL.1

Any workaround to this?

Update 2: Possible workaround?

I discovered if I slap $s->log->trace('print this'); before the apply_all_roles method is called, everything works as expected. Is there a better way?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: apply_all_roles method clobbering MooseX::Log::Log4perl
by 1nickt (Canon) on Jan 18, 2018 at 15:43 UTC

    Don't you need a closing brace at the end of your BUILD sub? (Edit fixed in OP via ninja edit)

    Why are you not consuming BarCollector with with as you do with the logger?

    Hope this helps!


    The way forward always starts with a minimal test.

      Bracket was accidentally not pasted in. It is not added with 'with' because it would typically be conditionally added. I didn't show that logic because it's not pertinent to the question.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

        I have not used Log4Perl nor do I use Moose any more. However, I use Role::Tiny and Moo, and when you apply a Role to an object manually, the object is reblessed into the resultant class, which will have a name something like 'Foo::Bar_WITH_Bar::Baz', so if your logger config is dependent on the class name, that could be the source of the problem.

        Things I would think are "pertinent to the question":

        • Does the same behaviour occur when you consume the role with with?
        • Does the same behaviour occur when you apply the role elsewhere than in the BUILD sub? E.g. using after BUILD?
        • Does the same behaviour occur when you create a minimalist test case?
        • Does the same behaviour occur when using Role::Tiny::apply_roles_to_object?
        • Does the same behaviour occur when testing with a role other than the Log4Perl one?

        Hope this helps!


        The way forward always starts with a minimal test.