[ 
https://issues.apache.org/jira/browse/LOG4J2-3281?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17466101#comment-17466101
 ] 

Fábio Constantino commented on LOG4J2-3281:
-------------------------------------------

Hello again, apologies for the delay in my response but holidays got in the way.

I've reproduced your code on my side and the difference here is that in your 
example you are not using any custom appenders.

What this means in practice is that when parsing the appenders from your 
properties file, in method _parseAppender_ ln416 in _PropertiesConfiguration,_ 
the call to _manager.parseAppender_ in ln427 is going to return the appender 
properly built with the filter associated to it correctly as it is using a 
_ConsoleAppenderBuilder_ (for the CONSOLE appender in your example properties 
file) and here we can see that it is using the _parseAppenderFilters_ method __ 
mentioned in the issue description, storing the return value in a variable and 
then using it when creating the appender 
({_}ConsoleAppenderBuilder.parseAppender{_} ln134 and ln137).

 

In my case however, since I am using a custom appender, the call to 
_manager.parseAppender_ in ln427 of _PropertiesConfiguration_ is going to 
return _null_ and as such the appender is going to be built via the call 
(ln429) to {_}PropertiesConfiguration.buildAppender{_}, and in this case the 
filter is being parsed in ln457 where the return value is then lost.

 

For testing I simply added my custom appender entry at the top of your 
properties file:

 
{code:java}
log4j.appender.LOG_REQUEST_START_DB=testing.CustomAppender
log4j.appender.LOG_REQUEST_START_DB.filter.1=testing.NeutralFilterFixture
log4j.appender.LOG_REQUEST_START_DB.filter.1.onMatch=neutral
log4j.appender.LOG_REQUEST_START_DB.Target=System.out
log4j.appender.LOG_REQUEST_START_DB.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG_REQUEST_START_DB.layout.ConversionPattern=%d{yyyy-MM-dd 
HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.filter.1=testing.NeutralFilterFixture
log4j.appender.CONSOLE.filter.1.onMatch=neutral
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p 
%c{1}:%L - %m%nlog4j.appender.A1=org.apache.log4j.FileAppender

log4j.appender.A1.File=target/temp.A1
log4j.appender.A1.Append=false
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-5p %c{2} - %m%n
log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=target/temp.A2
log4j.appender.A2.Append=false
log4j.appender.A2.layout=org.apache.log4j.TTCCLayout
log4j.appender.A2.layout.DateFormat=ISO8601
log4j.logger.org.apache.log4j.xml=trace, A1
log4j.rootLogger=trace, LOG_REQUEST_START_DB, CONSOLE, A1, A2 {code}
 

and created the _CustomAppender_ class in my _testing_ package (I also placed 
your _NeutralFilterFixture_ class in there):
{code:java}
package testing;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

public class CustomAppender extends AppenderSkeleton {    

    @Override
    public void close() {
        // TODO Auto-generated method stub
    }    

    @Override
    public boolean requiresLayout() {
        // TODO Auto-generated method stub
        return false;
    }    

    @Override
    protected void append(LoggingEvent event) {
        // TODO Auto-generated method stub
    }
} {code}
 

> PropertiesConfiguration.buildAppender not adding filters to appender
> --------------------------------------------------------------------
>
>                 Key: LOG4J2-3281
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-3281
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Appenders, Configurators
>    Affects Versions: 2.17.0
>            Reporter: Fábio Constantino
>            Priority: Blocker
>
> When building an appender, the _parseAppenderFilters_ method correctly finds 
> my custom filter configuration in the properties file, builds it and returns 
> it, but the caller ({_}buildAppender{_} method) does nothing with it 
> resulting in the appender not having any filters added to it.
>  
> This is related to the linked issue - 
> [LOG4J2-3247|https://issues.apache.org/jira/browse/LOG4J2-3247] - where the 
> scenario is the same (properties file config):
> {code:java}
> log4j1.compatibility=true
> log4j.appender.LOG_REQUEST_START_DB=my.appender.class
> log4j.appender.LOG_REQUEST_START_DB.filter.ID=my.filter.class {code}
> the Filter class I'm working with is the following:
> {code:java}
> import org.apache.log4j.spi.Filter;
> import org.apache.log4j.spi.LoggingEvent;
> public class MonitorFilter extends Filter {
>     @Override
>     public int decide(LoggingEvent event) {
>         String requestId = (String)event.getMDC("requestId");
>         if (StringHelper.isNullOrEmpty(requestId))
>             return DENY;        
>         
>         if 
> (!MonitorScriptManager.getInstance().getMonitorScript().filter(event))
>             return DENY;
>         
>         return ACCEPT;
>     }
> } {code}
> I am using the following log4j dependencies:
> {code:java}
>         <dependency>
>             <groupId>org.apache.logging.log4j</groupId>
>             <artifactId>log4j-core</artifactId>
>             <version>2.17.0</version>
>         </dependency>
>         <dependency>
>             <groupId>org.apache.logging.log4j</groupId>
>             <artifactId>log4j-api</artifactId>
>             <version>2.17.0</version>
>         </dependency>
>         <dependency>
>             <groupId>org.apache.logging.log4j</groupId>
>             <artifactId>log4j-1.2-api</artifactId>
>             <version>2.17.0</version>
>         </dependency> {code}



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to