开发者

How to get Log4Perl to use separate files for each script?

In the FAQ for Log4perl it is explained how to write the config file to use separate files for ERROR and INFO.

This works great for one script or if one wants to mix several scripts ERROR and INFO into the same ERROR and INFO logs.

Question

I would very much like each script have its own ERROR and INFO files, and that I still are able to just use

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

Have I made log.conf correct?

And how should I initiate Log4perl in each script?

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::Pa开发者_开发技巧tternLayout
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


Solution:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜