![](/img/trans.png)
[英]How to add my own suffix to the log file being rotated in Perl using the log4perl module?
[英]How to log the file using Log::Log4perl module in perl
我是 perl 的初學者。我使用 Getopt::Long 模塊編寫添加兩個數字的腳本。使用 Log::Log4perl 模塊記錄以下腳本時遇到困難。 誰能幫我解決這個問題。
use strict;
use warnings;
use Getopt::Long;
use Log::Log4perl;
my $num1=<>;
my $num2=<>;
chomp($num1,$num2);
my $res=GetOptions("numone=i"=>\$num1,
"numtwo=i"=>\$num2);
my $add=$num1 + $num2;
print $add;
這是一個例子:
use feature qw(say);
use strict;
use warnings;
use Getopt::Long;
use Log::Log4perl qw(:easy);
my $conf = do { local $/; <DATA> };
Log::Log4perl::init( \$conf );
my $num1;
my $num2;
if (@ARGV == 0) {
print "Enter number1: ";
chomp($num1 = <>);
INFO( "Read number1 from STDIN: '$num1'" );
print "Enter number2: ";
chomp($num2=<>);
INFO( "Read number2 from STDIN: '$num2'" );
}
else {
GetOptions("numone=i" =>\$num1, "numtwo=i"=>\$num2)
or die "Bad command line options\n";
die "Number1 undefined\n" if !defined $num1;
die "Number2 undefined\n" if !defined $num2;
INFO( "Read number1 from ARGV: '$num1'" );
INFO( "Read number2 from ARGV: '$num2'" );
}
my $add = $num1 + $num2;
say "Result: $num1 + $num2 = $add";
__DATA__
log4perl.rootLogger=INFO, LOGFILE
log4perl.appender.LOGFILE=Log::Log4perl::Appender::File
log4perl.appender.LOGFILE.filename=mylog.log
log4perl.appender.LOGFILE.mode=append
log4perl.appender.LOGFILE.layout=PatternLayout
log4perl.appender.LOGFILE.layout.ConversionPattern=[%r] %F %L %c - %m%n
示例 session:
$ p.pl --numone=3 --numtwo=4
Result: 3 + 4 = 7
$ cat mylog.log
[0] ./p.pl 26 main - Read number1 from ARGV: '3'
[0] ./p.pl 27 main - Read number2 from ARGV: '4'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.