我有一个Perl脚本,该脚本正在调用AWS命令行以将文件上传到S3。它使用Log4Perl记录有关上载的信息。当上传很小(几秒钟)时,日志记录可以正常工作;但是,当文件很大且需要几分钟时,则不会记录任何内容。文件可以正常上传,但未创建日志文件。
这就是我的做法:
#get start time
my @dt = localtime;
my $TransferStartDate = strftime("%Y-%m-%d", @dt);
my $TransferStartTime = strftime("%H:%M:%S", @dt);
my $aws_command = "/path/to/aws/bin/aws s3 mv $local_file s3://" . $s3_bucket;
my $aws_status = system( $aws_command);
#get stop time
@dt = localtime;
my $TransferEndDate = strftime("%Y-%m-%d", @dt);
my $TransferEndTime = strftime("%H:%M:%S", @dt);
my $log_conf = "
log4perl.rootLogger = $log_level, LOG1
log4perl.appender.LOG1 = Log::Log4perl::Appender::File
log4perl.appender.LOG1.filename = /path/to/logs/aws_s3_log-$TransferEndDate.txt
log4perl.appender.LOG1.mode = append
log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n
";
Log::Log4perl::init(\$log_conf);
my $logger = Log::Log4perl->get_logger();
$logger->info( "Transfer command = $aws_command" );
$logger->info( "Transfer start = $TransferStartDate $TransferStartTime" );
$logger->info( "Transfer end = $TransferEndDate $TransferEndTime" );
#check return code
if ( $aws_status == 0 )
{
$logger->info( "Transfer completed successfully");
}
elsif ( $aws_status == 1 )
{
$errstring = "s3 transfer failed status = $aws_status";
$logger->error( $errstring );
}
elsif ( $aws_status == 2 )
{
$errstring = "Syntax error on AWS S3 command status = $aws_status";
$logger->error( $errstring );
}
elsif ( $aws_status == 130 )
{
$errstring = "transfer interrupted by Ctrl-C or other SIGINT status = $aws_status";
$logger->error( $errstring );
}
elsif ( $aws_status == 255 )
{
$errstring = "Command failed status = $aws_status";
$logger->error( $errstring );
}
elsif ( $aws_status == 512 )
{
$errstring = "s3 command formatted incorrectly status = $aws_status";
$logger->error( $errstring );
}
else
{
$errstring = "Error with AWS s3 command status = $aws_status";
$logger->error( $errstring );
}
有什么主意我做错了吗?使用系统拨打电话会导致Perl脚本停止,直到上传完成,对吧?
答案 0 :(得分:1)
由于您正在记录文件,因此我将在脚本的开头添加一个$|=1;
。
我还认为您应该在启动aws_command之前实例化logger,并使用``:
启动它$|=1;
my @dt = localtime;
my $TransferStartDate = strftime("%Y-%m-%d", @dt);
my $TransferStartTime = strftime("%H:%M:%S", @dt);
my $log_conf = "
log4perl.rootLogger = $log_level, LOG1
log4perl.appender.LOG1 = Log::Log4perl::Appender::File
log4perl.appender.LOG1.filename = /path/to/logs/aws_s3_log-$TransferStartDate.txt
log4perl.appender.LOG1.mode = append
log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n
";
Log::Log4perl::init(\$log_conf);
my $logger = Log::Log4perl->get_logger();
my $aws_command = "/path/to/aws/bin/aws s3 mv $local_file s3://" . $s3_bucket;
my $aws_output = `$aws_command`;
my $aws_status = $?;
#get stop time
@dt = localtime;
my $TransferEndDate = strftime("%Y-%m-%d", @dt);
my $TransferEndTime = strftime("%H:%M:%S", @dt);
$logger->info( "Transfer command = $aws_command" );
$logger->info( "Transfer result = $aws_result" );
不确定会解决您的问题,但可能会帮助您缩小诊断范围。