log4perl 一个带有两个附加程序和不同日志级别的记录器
log4perl one logger with two appenders and different loglevels
我正在尝试使用单个记录器登录到不同日志级别的两个不同文件:
perl.pl
use Log::Log4perl;
Log::Log4perl::init('log4perl.conf');
my $logger = Log::Log4perl->get_logger('test');
$logger->trace('trace');
$logger->debug('debug');
$logger->info('info');
$logger->warn('warn');
$logger->error('error');
$logger->fatal('fatal');
log4perl.conf
log4perl.logger.test = ALL, log, debug
log4perl.appender.debug = Log::Log4perl::Appender::File
log4perl.appender.debug.filename = debug.log
log4perl.appender.debug.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.debug.layout.ConversionPattern = [%d] %p %m %n
log4perl.appender.log = Log::Log4perl::Appender::File
log4perl.appender.log.filename = log.log
log4perl.appender.log.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.log.layout.ConversionPattern = [%d] %p %m %n
log4perl.appender.log.Filter = MatchWARN
# Filter to match level WARN
log4perl.filter.MatchWARN = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchWARN.LevelToMatch = WARN
log4perl.filter.MatchWARN.AcceptOnMatch = true
预期结果:
log.log:
[2018/08/30 15:06:22] WARN warn
[2018/08/30 15:06:22] ERROR error
[2018/08/30 15:06:22] FATAL fatal
debug.log:
[2018/08/30 15:06:22] TRACE trace
[2018/08/30 15:06:22] INFO info
[2018/08/30 15:06:22] DEBUG debug
[2018/08/30 15:06:22] WARN warn
[2018/08/30 15:06:22] ERROR error
[2018/08/30 15:06:22] FATAL fatal
实际结果:
log.log:
[2018/08/30 15:06:22] WARN warn
debug.log:
[2018/08/30 15:06:22] WARN trace
[2018/08/30 15:06:22] INFO info
[2018/08/30 15:06:22] DEBUG debug
[2018/08/30 15:06:22] WARN warn
[2018/08/30 15:06:22] ERROR error
[2018/08/30 15:06:22] FATAL fatal
我在这里找到了一个解决方案:
https://metacpan.org/pod/Log::Log4perl::Filter#DESCRIPTION
只需创建您自己的过滤器...
log4perl.filter.FilterWARN = sub { \
my %p = @_; \
if($p{log4p_level} eq "WARN" or \
$p{log4p_level} eq "ERROR" or \
$p{log4p_level} eq "FATAL") { \
return 1; \
} \
return 0; \
}
我正在尝试使用单个记录器登录到不同日志级别的两个不同文件:
perl.pl
use Log::Log4perl;
Log::Log4perl::init('log4perl.conf');
my $logger = Log::Log4perl->get_logger('test');
$logger->trace('trace');
$logger->debug('debug');
$logger->info('info');
$logger->warn('warn');
$logger->error('error');
$logger->fatal('fatal');
log4perl.conf
log4perl.logger.test = ALL, log, debug
log4perl.appender.debug = Log::Log4perl::Appender::File
log4perl.appender.debug.filename = debug.log
log4perl.appender.debug.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.debug.layout.ConversionPattern = [%d] %p %m %n
log4perl.appender.log = Log::Log4perl::Appender::File
log4perl.appender.log.filename = log.log
log4perl.appender.log.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.log.layout.ConversionPattern = [%d] %p %m %n
log4perl.appender.log.Filter = MatchWARN
# Filter to match level WARN
log4perl.filter.MatchWARN = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchWARN.LevelToMatch = WARN
log4perl.filter.MatchWARN.AcceptOnMatch = true
预期结果:
log.log:
[2018/08/30 15:06:22] WARN warn
[2018/08/30 15:06:22] ERROR error
[2018/08/30 15:06:22] FATAL fatal
debug.log:
[2018/08/30 15:06:22] TRACE trace
[2018/08/30 15:06:22] INFO info
[2018/08/30 15:06:22] DEBUG debug
[2018/08/30 15:06:22] WARN warn
[2018/08/30 15:06:22] ERROR error
[2018/08/30 15:06:22] FATAL fatal
实际结果:
log.log:
[2018/08/30 15:06:22] WARN warn
debug.log:
[2018/08/30 15:06:22] WARN trace
[2018/08/30 15:06:22] INFO info
[2018/08/30 15:06:22] DEBUG debug
[2018/08/30 15:06:22] WARN warn
[2018/08/30 15:06:22] ERROR error
[2018/08/30 15:06:22] FATAL fatal
我在这里找到了一个解决方案: https://metacpan.org/pod/Log::Log4perl::Filter#DESCRIPTION 只需创建您自己的过滤器...
log4perl.filter.FilterWARN = sub { \
my %p = @_; \
if($p{log4p_level} eq "WARN" or \
$p{log4p_level} eq "ERROR" or \
$p{log4p_level} eq "FATAL") { \
return 1; \
} \
return 0; \
}