Reputation: 2944
I'm using Log::Log4perl to create log files, but it is appending to a single log file; instead, I want to create a separate log file for each execution of my script.
How can I create separate log files ?
Here is my code :
fetch.pl
#Opening Log configuration file
Log::Log4perl::init('./logs/log4perl.conf');
my $logger = Log::Log4perl->get_logger('./logs_$$.logs');
logs.conf
log4perl.logger = TRACE, FileAppndr1
log4perl.logger.logs = DEBUG, FileAppndr1
log4perl.appender.FileAppndr1 = Log::Log4perl::Appender::File
log4perl.appender.FileAppndr1.filename = logs.log
log4perl.appender.FileAppndr1.layout = Log::Log4perl::Layout::SimpleLayout
Upvotes: 2
Views: 547
Reputation: 69264
The logfile name is set in the config file, not in the call to get_logger()
. So you need to find out how to use variables in the config file. The Log4Perl FAQ contains the question "What if I need dynamic values in a static Log4perl configuration file?"
The important part of the answer is:
As of Log::Log4perl 0.28, every value in the configuration file can be specified as a Perl hook. So, instead of saying
log4perl.appender.Logfile.filename = test.log
you could just as well have a Perl subroutine deliver the value dynamically:
log4perl.appender.Logfile.filename = sub { logfile(); };
given that logfile() is a valid function in your main package returning a string containing the path to the log file.
So it looks like you want:
log4perl.appender.FileAppndr1.filename = sub { "logs_$$.log" };
Upvotes: 1
Reputation: 6355
To get variable interpolation in strings you'll have to use double quotes:
my $logger = Log::Log4perl->get_logger("./logs_$$.logs");
Or concatenation:
my $logger = Log::Log4perl->get_logger('./logs_' . $$ . '.logs');
Upvotes: 2