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 );

最新文章

  1. Lind.DDD.Utils.HttpHelper关于对HttpClient的正确使用
  2. 如何做一份能忽悠投资人的PPT
  3. MySql开启事务
  4. python之路 之open
  5. C# DateTime详解
  6. Recommended you 3 most popular Nissan pincode calculators
  7. powershell.exe直接运行命令
  8. 《STL源码剖析》chapter2空间配置器allocator
  9. sealed 密封类,不能被其他类继承,但可以继承其他类
  10. Eclipse debug ‘Source not found’
  11. CVE-2014-1767 漏洞分析(2015.1)
  12. [第一阶段] Python学习
  13. python写的一段分页的代码
  14. 虚拟机下linux系统安装nginx
  15. SparseArray到底哪点比HashMap好
  16. 开发过程中,ps要做的事情
  17. java 错误
  18. bzoj 4289 Tax - 最短路
  19. JMeter出现“the target server failed to respond“的解决办法
  20. IIS------项目配置到IIS后报500错误

热门文章

  1. asp.net MVC动态路由
  2. jQuery中的.bind()、.live()和.delegate()之间区别分析
  3. TableView的执行流程 &amp; 位移枚举
  4. UML类图之类与类的关系
  5. mongodb在WEB开发中的应用与实践
  6. 【翻译】Tomcat 6.0 部署与发布
  7. 升级xcode7.0 第三方库不能用的解决方法(bitcode是什么鬼?)
  8. 必须知道的.net——学习笔记1
  9. POJ 3292 Semi-prime H-numbers
  10. 【PHP面向对象(OOP)编程入门教程】3.什么是面向对象编程呢?