2

Log::Log4perlを使用してログ ファイルを作成していますが、単一のログ ファイルに追加しています。代わりに、スクリプトの実行ごとに個別のログ ファイルを作成したいと考えています。

別のログ ファイルを作成するにはどうすればよいですか?

これが私のコードです:

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
4

2 に答える 2

1

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" };
于 2014-04-03T14:31:39.617 に答える