如何在 Log::Log4perl 中设置两个不同日志级别的 appender?

How to set two appenders with different log levels in Log::Log4perl?

我有一个带有两个附加程序(一个屏幕和另一个文件)的记录器。我想要一个屏幕附加器,它具有可以更改的可变日志级别,以及无论如何都会记录所有内容的文件附加器。因此,例如,您可以禁用任何输出到屏幕(屏幕附加程序),但在日志文件(文件附加程序)中获取完整的日志记录到 TRACE 级别。我成功地更改了 Screen appender,但无法将同一记录器的 File appender 设置为 TRACE 级别。我尝试使用不同的阈值设置但没有成功。

# Define a category logger
my $log = Log::Log4perl->get_logger("main");

# Define a layout
my $layout = Log::Log4perl::Layout::PatternLayout->new("[%d{yyyy/MM/dd HH:mm:ss,SSS}]%m%n");

# Define a file appender
my $file_appender = Log::Log4perl::Appender->new(
                    "Log::Log4perl::Appender::File",
                    name      => "Logfile",
                    filename  => "$logfile",
                    autoflush => 1,
                    umask => 022,
                    header_text => "INVOCATION:[=11=] @ARGV", 
                    #Threshold => "TRACE",  DOES NOT WORK
                );

# Define a stderr appender
my $stderr_appender =  Log::Log4perl::Appender->new(
                    "Log::Log4perl::Appender::ScreenColoredLevels",
                    name      => "Screen",
                    stderr    => 1,
                );

# Have both appenders use the same layout (could be different)
$stderr_appender->layout($layout);
$file_appender->layout($layout);

#add both appenders to logger
$log->add_appender($stderr_appender);
$log->add_appender($file_appender);

#add a level to logger
#$log_level coming from command line or configuration
$log->level($log_level);

#$file_appender->threshold( "TRACE" );   THIS DOES NOT WORK
#Log::Log4perl->appender_thresholds_adjust(-1, ['Logfile']);   NOR THIS

#check your appenders
#print Dumper( Log::Log4perl->appenders() );   

来自log4perl FAQ

I want to log ERROR and WARN messages to different files! How can I do that?

Let's assume you wanted to have each logging statement written to a different file, based on the statement's priority. Messages with priority WARN are supposed to go to /tmp/app.warn, events prioritized as ERROR should end up in /tmp/app.error.

Now, if you define two appenders AppWarn and AppError and assign them both to the root logger, messages bubbling up from any loggers below will be logged by both appenders because of Log4perl's message propagation feature. If you limit their exposure via the appender threshold mechanism and set AppWarn's threshold to WARN and AppError's to ERROR, you'll still get ERROR messages in AppWarn, because AppWarn's WARN setting will just filter out messages with a lower priority than WARN -- ERROR is higher and will be allowed to pass through.

What we need for this is a Log4perl Custom Filter, available with Log::Log4perl 0.30.

Both appenders need to verify that the priority of the oncoming messages exactly matches the priority the appender is supposed to log messages of. To accomplish this task, let's define two custom filters, MatchError and MatchWarn, which, when attached to their appenders, will limit messages passed on to them to those matching a given priority:

   log4perl.logger = WARN, AppWarn, 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 WARN
    log4perl.filter.MatchWarn  = Log::Log4perl::Filter::LevelMatch
    log4perl.filter.MatchWarn.LevelToMatch  = WARN
    log4perl.filter.MatchWarn.AcceptOnMatch = true
        # Error appender
    log4perl.appender.AppError = Log::Log4perl::Appender::File
    log4perl.appender.AppError.filename = /tmp/app.err
    log4perl.appender.AppError.layout   = SimpleLayout
    log4perl.appender.AppError.Filter   = MatchError
        # Warning appender
    log4perl.appender.AppWarn = Log::Log4perl::Appender::File
    log4perl.appender.AppWarn.filename = /tmp/app.warn
    log4perl.appender.AppWarn.layout   = SimpleLayout
    log4perl.appender.AppWarn.Filter   = MatchWarn

The appenders AppWarn and AppError defined above are logging to /tmp/app.warn and /tmp/app.err respectively and have the custom filters MatchWarn and MatchError attached. This setup will direct all WARN messages, issued anywhere in the system, to /tmp/app.warn (and ERROR messages to /tmp/app.error) -- without any overlaps.

另请查看 log4perl 及其子模块的 CPAN 文档:

http://search.cpan.org/~mschilli/Log-Log4perl-1.46/lib/Log/Log4perl/Filter.pm