mschilli / log4perl

Log4j Implementation For Perl
http://log4perl.com
Other
116 stars 66 forks source link

Avoiding warning when init() is not called #91

Closed targzeta closed 3 years ago

targzeta commented 5 years ago

Hi, I have written a module that actually uses Log4Perl as logging system. This is how I use Log4Perl:

package MyPackage;

use Log::Log4perl;

my $logger = Log::Log4perl->get_logger();
...
$logger->info("foo bar");

Now, I don't know if the logger it was initialized or not by the main program. If not, the main program receives:

Log4perl: Seems like no initialization happened. Forgot to call init()?

Is there a transparent way to use Log4perl only if the main has configured it? So that the get_logger() returns always an instance that doesn't do nothing.

Actually I have in the main program:

use Log::Log4perl;
$Log::Log4perl::Logger::INITIALIZED = 1;

but I think that a module should be independent from who use it.

Thank you in advance, Emanuele

mohawk2 commented 3 years ago

There's an FAQ entry "My new module uses Log4perl -- but what happens if the calling program didn't configure it?". The answer is:

If a Perl module uses Log::Log4perl, it will typically rely on the calling program to initialize it. If it is using Log::Log4perl in :easy mode, like in

package MyMod;
use Log::Log4perl qw(:easy);

sub foo {
    DEBUG("In foo");
}

1;

and the calling program doesn't initialize Log::Log4perl at all (e.g. because it has no clue that it's available), Log::Log4perl will silently ignore all logging messages.