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

Hello, I am trying to setup a logger that has multiple appenders/categories (for example, one that goes to a file and one that goes to stdout). However, I also need to be able to modify the appenders after the initialization. For example, I want to update the log level and update the file name after initialization. I have found several examples of how to create the logger (and I have been successful in creating and using them), but I cannot figure out how to modify the appenders. I have worked through the documentation and help on these sites:
https://metacpan.org/pod/Log::Log4perl https://www.perl.com/pub/2002/09/11/log4perl.html/ https://www.perlmonks.org/?node_id=1198853
but I cannot find a combination that works. Lets say I have this:
use Log::Log4perl; sub init() { Log::Log4perl->easy_init( { level => $DEBUG, file => ">>test.log", category => "FILE" }, { level => $DEBUG, file => "STDOUT", category => "STDOUT" } ); } sub getLogger { my ($category) = @_; my $logger = Log::Log4perl->get_logger($category); return $logger; } init(); my $stdout_logger = getLogger(STDOUT); $stdout_logger->info("testing stdout logger"); my $file_logger = getLogger(FILE); $file_logger->info("testing file logger");
How do I then change the level and file for each of these appenders? I have tried these example:
Log::Log4perl::Logger::APPENDER_BY_NAME{'STDOUT'}->threshold($TRACE); Log::Log4perl->appender_by_name('STDOUT')->threshold($TRACE);
but they did not work. Alternatively, I tried this example:
https://metacpan.org/pod/Log::Log4perl#Advanced-configuration-within-P +erl
and I got the logging to work, but I could not change the appender. What am I missing or doing wrong? Thanks

-- UPDATE --

http://log4perl.sourceforge.net/releases/Log-Log4perl/docs/html/Log/Log4perl/Appender/File.html

Here is how you change the file in an appender at run time:

<code> Log::Log4perl->appender_by_name('FILE')->file_switch('test2.log'); <code>

Replies are listed 'Best First'.
Re: log4perl help
by Anonymous Monk on Mar 03, 2021 at 00:20 UTC

    What am I missing or doing wrong?

    Hi,

    The program as posted is incomplete . Adding strict/warning reveals problems.

    But, the name of your appenders is not the same as the name of the category

    print join " ", keys %{ Log::Log4perl->appenders() }, "\n";

    returns app001 app002

    easy_init is only so easy ;)

      ok, so how do I name the appenders when using easy_init? I tried:
      Log::Log4perl->easy_init( { name => 'file_appender' level => $DEBUG, file => ">>test.log", category => "FILE" }, { name => 'stdout_appender' level => $DEBUG, file => "STDOUT", category => "STDOUT" } );
      but that did not work. If easy_init does not support naming the appenders, how do I create an initializer with named appenders that I can access and change within code? Thanks
        Use the init method, example in doc