Log4perl 的使用

Perl 使用Log4perl

首先下载log4 module :

http://search.cpan.org/CPAN/authors/id/M/MS/MSCHILLI/Log-Log4perl-1.43.tar.gz

解压配置:

这里只用到lib目录下的文件,可以将其他目录删除。

如何使用这个模块:

新增一个名为log4perl.conf的文件,放在lib目录下

配置如下:

这里使用自定义的package: log4用来设置log路径

 log4perl.category.Foo.Bar=DEBUG,Logfile,Screen
log4perl.rootLogger=DEBUG,Logfile,Screen log4perl.appender.Logfile=Log::Log4perl::Appender::File
log4perl.appender.Logfile.filename=\
sub { return &log4::set_log_name(); }
log4perl.appender.Logfile.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.Logfile.layout.ConversionPattern =%d{yyyy/M/d HH:mm:ss} %F %L - %m%n log4perl.appender.Screen = Log::Log4perl::Appender::Screen
log4perl.appender.Screen.stderr = 0
log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Screen.layout.ConversionPattern =%d{yyyy/M/d HH:mm:ss} %F %L - %m%n

使用log4perl.conf

#!/usr/bin/perl
package log4;
use strict;
use FindBin qw($Bin);
use lib "$Bin/lib";
print "$Bin\n";
use Log::Log4perl qw(get_logger);
my $log = get_logger(__PACKAGE__);
my $log_name="log4.log";
my $log_conf = "$Bin/lib/log4perl.conf";
print "$log_conf\n"; # Check config every 60 secs
Log::Log4perl::init_and_watch($log_conf,); $log->debug("Debug message");
$log->info("Info message");
$log->error("Error message");
$log->info(&ping);
sub ping
{ my @ping=readpipe("ping 127.0.0.1");
return @ping;
}
sub set_log_name
{
return $log_name;
}

其中:

$log->trace("..."); # Log a trace message
$log->debug("..."); # Log a debug message
$log->info("..."); # Log a info message
$log->warn("..."); # Log a warn message
$log->error("..."); # Log a error message
$log->fatal("..."); # Log a fatal message

运行结果如下:

D:\perl>perl log4test.pl
D:/perl
D:/perl/lib/log4perl.conf
2014/4/22 14:40:29 log4test.pl 17 - Debug message
2014/4/22 14:40:29 log4test.pl 18 - Info message
2014/4/22 14:40:29 log4test.pl 19 - Error message
2014/4/22 14:40:33 log4test.pl 20 -
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64

Ping statistics for 127.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms

生成log内容是:

2014/4/22 14:40:29 log4test.pl 17 - Debug message
2014/4/22 14:40:29 log4test.pl 18 - Info message
2014/4/22 14:40:29 log4test.pl 19 - Error message
2014/4/22 14:40:33 log4test.pl 20 -
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64

Ping statistics for 127.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms

也可以直接将配置文件放入脚本

 my $conf = q(
log4perl.category.Foo.Bar = INFO, Logfile, Screen log4perl.appender.Logfile = Log::Log4perl::Appender::File
log4perl.appender.Logfile.filename = test.log
log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Logfile.layout.ConversionPattern = [%r] %F %L %m%n log4perl.appender.Screen = Log::Log4perl::Appender::Screen
log4perl.appender.Screen.stderr =
log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout
); # ... passed as a reference to init()
Log::Log4perl::init( \$conf );
上一篇:深入刨析tomcat 之---第2篇,解决第3章bug 页面不显示内容http://localhost:8080/servlet/ModernServlet?userName=zhangyantao&password=1234


下一篇:使用静态函数impl模式做接口