如何让Log4Perl为每个脚本使用单独的文件?

时间:2011-07-07 13:44:48

标签: perl

在Log4perl的FAQ中,解释了如何编写配置文件以使用单独的文件用于ERROR和INFO。

这适用于一个脚本,或者如果想要将几个脚本ERROR和INFO混合在一起 相同的ERROR和INFO日志。

问题

我非常希望每个脚本都有自己的ERROR和INFO文件,而且我仍然可以使用

$logger->info()
$logger->error()

我的log.conf是否正确?

我应该如何在每个脚本中启动Log4perl?

account.pl

use Log::Log4perl;
Log::Log4perl::init('log.conf');
my $logger = Log::Log4perl->get_logger('????');

log.conf

log4perl.logger.account = INFO, AccountErrorLogFile, AccountInfoLogFile, AccountInfoLogFile, SystemErrorLogFile, testScreen

# Filter to match level ERROR
log4perl.filter.MatchError = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchError.LevelToMatch  = ERROR
log4perl.filter.MatchError.AcceptOnMatch = true

# Filter to match level INFO
log4perl.filter.MatchInfo  = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchInfo.LevelToMatch  = INFO
log4perl.filter.MatchInfo.AcceptOnMatch = true

# Error appender
log4perl.appender.AccountErrorLogFile                          = Log::Log4perl::Appender::File
log4perl.appender.AccountErrorLogFile.filename                 = /logs/error--account-pl.log
log4perl.appender.AccountErrorLogFile.layout                   = Log::Log4perl::Layout::PatternLayout
log4perl.appender.AccountErrorLogFile.layout.ConversionPattern = %d [%3L] %m%n
log4perl.appender.AccountErrorLogFile.Filter                   = MatchError

# Info appender
log4perl.appender.AccountInfoLogFile                          = Log::Log4perl::Appender::File
log4perl.appender.AccountInfoLogFile.filename                 = /logs/info--account-pl.log
log4perl.appender.AccountInfoLogFile.layout                   = Log::Log4perl::Layout::PatternLayout
log4perl.appender.AccountInfoLogFile.layout.ConversionPattern = %d [%3L] %m%n
log4perl.appender.AccountInfoLogFile.Filter                   = MatchInfo

# Error appender
log4perl.appender.SystemErrorLogFile                          = Log::Log4perl::Appender::File
log4perl.appender.SystemErrorLogFile.filename                 = /logs/error--system-pl.log
log4perl.appender.SystemErrorLogFile.layout                   = Log::Log4perl::Layout::PatternLayout
log4perl.appender.SystemErrorLogFile.layout.ConversionPattern = %d [%3L] %m%n
log4perl.appender.SystemErrorLogFile.Filter                   = MatchError

# Info appender
log4perl.appender.SystemInfoLogFile                          = Log::Log4perl::Appender::File
log4perl.appender.SystemInfoLogFile.filename                 = /logs/info--system-pl.log
log4perl.appender.SystemInfoLogFile.layout                   = Log::Log4perl::Layout::PatternLayout
log4perl.appender.SystemInfoLogFile.layout.ConversionPattern = %d [%3L] %m%n
log4perl.appender.SystemInfoLogFile.Filter                   = MatchInfo

# Development Appender
log4perl.appender.testScreen                          = Log::Log4perl::Appender::Screen
log4perl.appender.testScreen.stderr                   = 0
log4perl.appender.testScreen.layout                   = Log::Log4perl::Layout::PatternLayout
log4perl.appender.testScreen.layout.ConversionPattern = %-5p [%3L] %m%n

1 个答案:

答案 0 :(得分:6)

解决方案:

<强> log.conf

log4perl.logger.account = INFO, AccountErrorLogFile, AccountInfoLogFile, testScreen
log4perl.logger.system  = INFO, SystemErrorLogFile, SystemInfoLogFile, testScreen

<强> account.pl

Log::Log4perl::init('/var/www/useradmin/cgi-bin/log.conf');
my $logger = Log::Log4perl->get_logger('account');

<强> system.pl

Log::Log4perl::init('/var/www/useradmin/cgi-bin/log.conf');
my $logger = Log::Log4perl->get_logger('system');