Perl:将 (Log4perl-) 对象传递给模块
Perl: Pass on (Log4perl-)object to module
我开始欣赏 Log4perl,我想在主要的 Perl 脚本和(几个)模块中使用它。我如何实施它,最好以面向对象的方式实施?
是否可以将(主脚本的)记录器对象与模块的对象连接起来,以便完整记录主脚本中发生的事件以及模块(的对象)的事件?
假设我有这样的主脚本:
use rotate_action;
use Log::Log4perl;
my $logger = get_logger();
my $appender_log = Log::Log4perl::Appender->new(
"Log::Dispatch::File",
filename => "action.log",
mode => "append"
);
$logger->add_appender($appender_log);
$logger->info("Logger activated");
use rotate_action;
my $ro = Rotation->new; # package in rotate_action.pm
#associate logger-object with ro-object here:
$ro->$logger; # pseudo-code!
my $file "somefile";
$logger->info("processing $file");
$ro->process_file("$file");
$logger->info("finished processing $file);
还有一个像这样的模块rotate_action
:
{
package Rotation;
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
return $self;
}
sub process_file {
my $self = shift;
my $file = shift;
my $exec_string = "identify -format \"orientation: %[orientation]\ngeometry: %g\n\"";
$exec_string .= " $file";
my $exec_result = `$exec_string 2>&1`;
my $err_lvl = $?;
if ($err_lvl != 0) {
#put same logger-object from main script here:
$self->$logger->warn("$file is not an image"); # pseudo-code!
} else {
#put same logger-object from main script here:
$self->$logger->info("rotate $file"); # pseudo-code!
`rotate.sh $file`;
}
}
}
如何将记录器对象传递给模块以写入相同的日志文件(如在主脚本中配置的那样)?
您可以向对象添加记录器字段并将记录器存储在那里:
sub add_logger {
my ($self, $logger) = @_;
$self->{logger} = $logger;
}
这会被称为
$ro->add_logger($logger);
然后你就可以
$self->{logger}->warn("$file is not an image");
或者您可以直接向构造函数提供记录器。
我开始欣赏 Log4perl,我想在主要的 Perl 脚本和(几个)模块中使用它。我如何实施它,最好以面向对象的方式实施? 是否可以将(主脚本的)记录器对象与模块的对象连接起来,以便完整记录主脚本中发生的事件以及模块(的对象)的事件?
假设我有这样的主脚本:
use rotate_action;
use Log::Log4perl;
my $logger = get_logger();
my $appender_log = Log::Log4perl::Appender->new(
"Log::Dispatch::File",
filename => "action.log",
mode => "append"
);
$logger->add_appender($appender_log);
$logger->info("Logger activated");
use rotate_action;
my $ro = Rotation->new; # package in rotate_action.pm
#associate logger-object with ro-object here:
$ro->$logger; # pseudo-code!
my $file "somefile";
$logger->info("processing $file");
$ro->process_file("$file");
$logger->info("finished processing $file);
还有一个像这样的模块rotate_action
:
{
package Rotation;
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
return $self;
}
sub process_file {
my $self = shift;
my $file = shift;
my $exec_string = "identify -format \"orientation: %[orientation]\ngeometry: %g\n\"";
$exec_string .= " $file";
my $exec_result = `$exec_string 2>&1`;
my $err_lvl = $?;
if ($err_lvl != 0) {
#put same logger-object from main script here:
$self->$logger->warn("$file is not an image"); # pseudo-code!
} else {
#put same logger-object from main script here:
$self->$logger->info("rotate $file"); # pseudo-code!
`rotate.sh $file`;
}
}
}
如何将记录器对象传递给模块以写入相同的日志文件(如在主脚本中配置的那样)?
您可以向对象添加记录器字段并将记录器存储在那里:
sub add_logger {
my ($self, $logger) = @_;
$self->{logger} = $logger;
}
这会被称为
$ro->add_logger($logger);
然后你就可以
$self->{logger}->warn("$file is not an image");
或者您可以直接向构造函数提供记录器。