我有一个脚本“server.pl”,它在后台运行并且使用 Log4Perl 进行自我记录。
该脚本不断读取目录并检测使用 Linux::Inotify2 模块在其中创建的新文件。
每个检测到的文件都是一个可存储的对象,它是一个要运行的管道,并且必须写入自己的日志文件。我的问题是,当我调用 Log4Perl::init 来初始化管道的记录器时, server.pl 不再记录自己,因为新的初始化已经覆盖了前一个。所以问题是如何让“server.pl”脚本保存多个Log4Perl 实例(预先未定义的数量)?
下面是“server.pl”的截断版本(没有无聊的东西)
#!/usr/bin/perl
use warnings;
use strict;
use Carp;
use Log::Log4perl;
use Linux::Inotify2;
use Storable;
Log::Log4perl->init('/.../log4perl.conf');
my $server_logger = Log::Log4perl->get_logger("server");
my $tracker = PipelineBatchTracker->new( _logger => $server_logger);
my $inotify = new Linux::Inotify2()
or $server_logger->logcroak("Unable to create new inotify object: $!");
# Sets non-blocking inotify
$inotify->blocking(0);
# define watcher
$inotify->watch
(
"/.../serial/",
IN_CREATE,
sub {
my $e = shift;
my $pipe;
my $serial = $e->{name};
my $full = $e->{w}{name} . ${serial};
$server_logger->info(${serial} . " was created in " . $e->{w}{name}) if $e->IN_CREATE;
eval {
my $pipe = retrieve("${full}");
$server_logger->logcroak("Unable to retrieve storable object") unless defined $pipe;
$server_logger->info(${serial} . " loaded into a Synegie::Pipeline object");
# This methods call Log::Log4perl->init
# and this is bad cause the server and former
# running pipelines are not logging anymore !!!
$pipe->setLogger();
$tracker->addPipeline($pipe);
};
if ($@) {
$server_logger->error("server : Failed to add pipeline : $@");
}
}
);
while (1) {
$server_logger->trace("--------------------- AND AGAIN -------------------------");
$inotify->poll;
sleep 2;
eval {
$tracker->poke();
};
if ($@) {
$server_logger->error("server : $@");
}
sleep 30;
}
编辑:在全球范围内,这意味着我需要一个取决于每个实例的记录器,而不是仅在脚本级别定义的记录器。有任何想法吗 ?谢谢