一个好老好老的老程序员了。
全部博文(915)
分类: Python/Ruby
2011-04-03 11:24:16
在我们没有把Perl的Log4perl库加到Perl的@INC配置中时,我们会得到下面的错误:
Can't locate Log/Log4perl.pm in @INC (@INC contains: /usr/lib/perl5/5.8.5/i386-linux-thread-multi /usr/lib/perl5/5.8.5 /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.4/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl/5.8.4 /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5/site_perl/5.8.2 /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.4/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl/5.8.4 /usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl/5.8.2 /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at xxxx.pl line 2.
BEGIN failed--compilation aborted at xxxx.pl line 2.
解决办法:在源码的第一行家上use lib 'Log4perl lib path';如:use lib '/tmp/Log-Log4perl-1.32/lib';(我解压完的目录在/tmp下)。
然后我们运行能得到下面的结果:
2011/04/03 13:17:43 No drink defined
因为我们设置的级别是ERROR,所以只打印出了ERROR界别的信息。将Log::Log4perl->easy_init($ERROR);
改为Log::Log4perl->easy_init($INFO);就可以输出两条信息了。
下面我说说通过配置文件来配置。
log4perl.conf文件内容如下:
然后修改代码:
在控制台我们会看到如下输出:
[6 ] ERROR main 2011/04/03 13:23:01 - No drink defined
[7 ] INFO main 2011/04/03 13:23:01 - Drinking Soda
同时在/var/log/myerrs.log文件中也产生上面输出的内容。
关于配置文件中的参数说明如下:
[quote]
%c Category of the logging event. %C Fully qualified package (or class) name of the caller %d Current date in yyyy/MM/dd hh:mm:ss format %F File where the logging event occurred %H Hostname (if Sys::Hostname is available) %l Fully qualified name of the calling method followed by the callers source the file name and line number between parentheses. %L Line number within the file where the log statement was issued %m The message to be logged %m{chomp} The message to be logged, stripped off a trailing newline %M Method or function where the logging request was issued %n Newline (OS-independent) %p Priority of the logging event %P pid of the current process %r Number of milliseconds elapsed from program start to logging event %R Number of milliseconds elapsed from last logging event to current logging event %T A stack trace of functions called %x The topmost NDC (see below) %X{key} The entry 'key' of the MDC (see below) %% A literal percent (%) sign[/quote]关于Log4perl配置不同日志级别输出到不同的地方,在后续的文章中我们再一起研究。
Log4perl使用(二)http://blog.chinaunix.net/space.php?uid=78707&do=blog&id=205550