如何在运行时更改 log4perl 追加器的过滤器?



我一直在试图弄清楚我是否可以在我通过配置文件定义的运行时更改Appender的过滤器。

log4perl.filter.M1               = Log::Log4perl::Filter::LevelMatch
log4perl.filter.M2               = Log::Log4perl::Filter::LevelMatch
log4perl.filter.M1.LevelToMatch  = INFO
log4perl.filter.M1.AcceptOnMatch = true
log4perl.filter.M2.LevelToMatch  = WARN
log4perl.filter.M2.AcceptOnMatch = true
log4perl.filter.MyBoolean0       = Log::Log4perl::Filter::Boolean
log4perl.filter.MyBoolean0.logic = M1
log4perl.filter.MyBoolean1       = Log::Log4perl::Filter::Boolean
log4perl.filter.MyBoolean1.logic = M1 || M2
log4perl.appender.SCREEN.Filter  = MyBoolean0 

我想将此过滤器从MyBoolean0SCREEN更改为MyBoolean1,但是在我的程序开始运行后进行。

使用data :: Dumper在APPENDER_BY_NAME哈希上戳CC_4 Hash,显示以下内容:

$VAR1 = bless( {
     'appender' => bless( {
                            'Filter' => 'MyBoolean0',
                            'color' => {
...
...
     'filter' => bless( {·
                          'params' => {·
                                        'M3' => bless( {·
                                                         'LevelToMatch' => 'ERROR',
                                                         'name' => 'M3',
                                                         'AcceptOnMatch' => 1
                                                       }, 'Log::Log4perl::Filter::LevelMatch' ),
                                        'M1' => bless( {·
                                                         'LevelToMatch' => 'INFO',
                                                         'name' => 'M1',
                                                         'AcceptOnMatch' => 1
                                                       }, 'Log::Log4perl::Filter::LevelMatch' ),
                                        'M2' => bless( {·
                                                         'LevelToMatch' => 'WARN',
                                                         'name' => 'M2',
                                                         'AcceptOnMatch' => 1
                                                       }, 'Log::Log4perl::Filter::LevelMatch' )
                                      },
                          'name' => 'MyBoolean0',
                          'eval_func' => sub { "DUMMY" },
                          'logic' => 'M1 || M2 || M3'
                        }, 'Log::Log4perl::Filter::Boolean' ),
     'warp_message' => undef,
     'name' => 'SCREEN'
   }, 'Log::Log4perl::Appender' );

但是,对我来说,对我来说似乎很刺耳。有没有更好的方法来更改Appender的过滤器?

您可以使用无证Appender的属性filter

$Log::Log4perl::Logger::APPENDER_BY_NAME{'SCREEN'}->filter(
    Log::Log4perl::Filter::by_name('MyBoolean1')
);

还可以使用两个附录:

log4perl.appender.SCREEN0.Filter = MyBoolean0
log4perl.appender.SCREEN1.Filter = MyBoolean1

并在运行时更改它:

$logger->remove_appender('SCREEN0', 1);
$logger->add_appender(
    Log::Log4perl::Config::create_appender_instance(
        $Log::Log4perl::Config::OLD_CONFIG,
        'SCREEN1',
        %Log::Log4perl::Logger::APPENDER_BY_NAME
    )
);

最新更新