如何在Log :: Log4perl中设置两个具有不同日志级别的附加程序? [英] How to set two appenders with different log levels in Log::Log4perl?

查看:156
本文介绍了如何在Log :: Log4perl中设置两个具有不同日志级别的附加程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个附加程序(一个屏幕和另一个文件)的记录器.我想要一个屏幕追加器,它的日志级别可以更改,文件追加器可以记录所有内容.因此,例如,您可以禁用任何输出到屏幕(屏幕追加器)的功能,但可以将完整日志记录到日志文件(文件追加器)中的TRACE级别.我成功更改了屏幕附加程序,但无法将同一记录器的文件附加程序设置为TRACE级别.我尝试使用其他阈值设置,但没有成功.

I have a logger with two appenders (one Screen and other File). I want to have a Screen appender with a variable log level that can be changed and File appender that will log everything no matter what. So for instance, you can disable any output to screen (Screen appender) but get full logging to the level of TRACE in your logfile (File appender). I succeeded in getting changing Screen appender but I can't set File appender for the same logger to the level of TRACE. I tried using different threshold settings but with no success.

# 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:$0 @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常见问题解答:

我想将错误和警告消息记录到不同的文件中!我该怎么办?

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

让我们假设您希望根据语句的优先级将每个日志记录语句写入不同的文件.优先级为WARN的消息应该转到/tmp/app.warn,优先级为ERROR的事件应在/tmp/app.error中结束.

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.

现在,如果您定义两个附加程序AppWarn和AppError并将它们都分配给根记录器,则由于Log4perl的消息传播功能,两个附加器都会记录下面的任何记录器冒泡的消息.如果您通过附加阈值机制限制它们的公开程度,并将AppWarn的阈值设置为WARN,而AppError的阈值设置为ERROR,您仍然会在AppWarn中收到ERROR消息,因为AppWarn的WARN设置只会过滤出优先级低于WARN的消息-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.

为此,我们需要一个Log4perl自定义过滤器,可用于Log :: Log4perl 0.30.

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

两个附加程序都需要验证即将到来的消息的优先级与该附加程序应该记录其消息的优先级完全匹配.为了完成此任务,让我们定义两个自定义过滤器,MatchError和MatchWarn,将它们附加到其追加程序后,会将传递给它们的消息限制为与给定优先级匹配的消息:

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

上面定义的附加程序AppWarn和AppError分别记录到/tmp/app.warn和/tmp/app.err,并附加了自定义过滤器MatchWarn和MatchError.此设置会将所有在系统中任何地方发出的WARN消息都定向到/tmp/app.warn(将ERROR消息定向到/tmp/app.error)–没有任何重叠.

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文档:

Also look at the CPAN docs for log4perl and its sub modules:

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

这篇关于如何在Log :: Log4perl中设置两个具有不同日志级别的附加程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆