logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Zart Colwing <Za...@wanadoo.fr>
Subject Is it me or FilterBasedTriggeringPolicy has a bug!
Date Thu, 31 Mar 2011 11:20:47 GMT

I recently was trying to use the FilterBasedTriggeringPolicy along with
a StringMatchFilter to trigger log file rolling upon the emission of a 
specific log event.

but whatever the value I give to StringMatchFilter's "acceptOnMatch" 
(see the log4j configuration at the end of the post), be it "false" 
or "true", I end-up with one file per log event !

I looked at the FilterBasedTriggeringPolicy's source code and see a 
strange looking isTriggeringEvent method:

  public boolean isTriggeringEvent(LoggingEvent event) {
    //   in the abnormal case of no contained filters
    //     always return true to avoid each logging event
    //     from having its own file.
    if (headFilter == null) {
      return false;                 << OK, could be optimized out

    //    otherwise loop through the filters
    for (Filter f = headFilter; f != null; f = f.next) {
      switch (f.decide(event)) {    << could be optimized
      case Filter.DENY:
        return false;

      case Filter.ACCEPT:
        return true;

    return true;                    << WTF!

I then wrote my own TriggeringPolicy implementation as follow 
(as a side note: I couldn't derive from FilterBasedTriggeringPolicy 
because it is final - god know why)

  public boolean isTriggeringEvent(final LoggingEvent event) {
    for(Filter f = headFilter; f != null; f = f.getNext()) {
      if(f.decide(event) == Filter.ACCEPT) {
        // if one filter accept the event then return true
        return true;
    // otherwise always return false to avoid each logging event
    // from having its own file
    return false;

Can someone take a look and tell me if the original implementation 
is on purpose or if it is a bug left behind ?

Here's the log4j configuration (classes FQN shortened by hand):

<appender name="file" class="o.a.l.rolling.RollingFileAppender">
  <rollingPolicy class="o.a.l.rolling.FixedWindowRollingPolicy">
    <param name="FileNamePattern" value="operations.%i.log" />

  <triggeringPolicy class="o.a.l.rolling.FilterBasedTriggeringPolicy">
    <filter class="org.apache.log4j.filter.StringMatchFilter">
      <param name="stringToMatch" value="*** log-rolling ***" />
      <param name="acceptOnMatch" value="false" />

  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d %-5p [%c{5}] %m%n" />


Zart Colwing.

To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org

View raw message