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>