Log4perl:使用easy_init分离INFO和ERROR文件

时间:2011-01-12 09:57:16

标签: perl log4perl

我目前正在使用以下内容向STDOUT发送INFO及以上信息:

Log::Log4perl->easy_init({level=>("$INFO"), layout=>"%d %p - %m%n", file=>"STDOUT"});

如何将ERROR及以上版本发送给STDERR?

1 个答案:

答案 0 :(得分:2)

此任务对于easy_init设置可能太多了,因为您需要使用过滤器来实现该效果。通过正常设置,您可以执行以下操作:

use Log::Log4perl qw(:easy);

Log::Log4perl->init(\ qq{
    log4perl.logger = INFO, AppInfo, AppError

    # 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.AppError = Log::Log4perl::Appender::Screen
    log4perl.appender.AppError.stderr   = 1
    log4perl.appender.AppError.layout   = SimpleLayout
    log4perl.appender.AppError.Filter   = MatchError

    # Info appender
    log4perl.appender.AppInfo = Log::Log4perl::Appender::Screen
    log4perl.appender.AppInfo.stderr   = 0
    log4perl.appender.AppInfo.layout   = SimpleLayout
    log4perl.appender.AppInfo.Filter   = MatchInfo
});

ERROR "Error";
INFO "Info";