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

In a module I'm working on, I have
use Moo; use Log::Any; has 'log' => ( is => 'ro', default => sub { Log::Any->get_logger( category => "unfolder" +) }, ); # and below $self->log->debug("bla");
So that in my tests I can get the message from unfolder in a file distinct from other parts. That's ok.

Now, I want to turn off the logging from the unfolder category. I thought that

has 'log' => ( is => 'ro', default => sub { Log::Any->get_logger( category => "unfolder" +, 'log_level' => "critical" ) }, ); #and below $self->log->debug("message");

for someone using my module with just

use Log::Any::Adapter( 'File', './log.txt' );

Would do the trick, but no, I still have all the messages from the unfolder part in the log.txt.

What am I missing ?

Thanks!

Replies are listed 'Best First'.
Re: Turning off logging
by 1nickt (Canon) on Oct 31, 2018 at 15:51 UTC

    Hi, you missed one very important part of the doc for Log::Any::Adapter::File:

    "Category is ignored."

    Also, note that the consumers of your log statements decide what they will consume by setting the log level, as well as the category (assuming the adapter they choose to use supports filtering by log level). So specifying a log level when defining the logger attribute of your object will have no effect: all you can control as the producer is the level of the message. Here's a simple demonstration:

    Module:

    package MyClass; use Log::Any (); use Moo; has log => ( is => 'ro', default => sub { Log::Any->get_logger(category => 'unfolder') }, ); sub greet { $_[0]->log->debug('HELO, WORLD'); }; 1; __END__
    Script:
    # 1224985.pl use strict; use warnings; use feature 'say'; use lib '.'; use MyClass; use Log::Any::Adapter; my $level = shift or die 'log level must be provided'; my $obj = MyClass->new; Log::Any::Adapter->set('Stdout', log_level => $level); say '>'; $obj->greet; say '<'; __END__
    Tests:
    $ perl 1224985.pl critical > < $ perl 1224985.pl debug > HELO, WORLD <

    Hope this helps!


    The way forward always starts with a minimal test.

      Thanks !

      I tried to use level:

      in my module

      $self->log-trace("this is useful for every users"): ... $self->log->debug("this is only useful during development");
      In a file using this module
      use Log::Any::Adapter( 'File', './log.txt', 'log_level'=>'trace' );

      Strange is that I still get the debug info in the file...

      How can I get only message send with trace ?

      Am I understanging the levels in the wrong direction ?

      Sorry, bare with me.

      Thanks

      F.
        Am I understanging the levels in the wrong direction ?

        I'm afraid so: trace is more verbose than debug. Switch them round and you should be good to go.