2

I've configured a SiftingAppender like this:

<appender name="FILE" class="ch.qos.logback.classic.sift.SiftingAppender">

    <discriminator>
        <key>context</key>
        <defaultValue>global</defaultValue>
    </discriminator>

    <!-- sift into different files -->
    <sift>
        <appender name="FILE-${context}" class="ch.qos.logback.core.FileAppender">
            <file>${logroot}/${context}.log</file>
            <encoder>
                <pattern>[%d{HH:mm:ss.SSS}] %-5level %logger{36} [%thread]%n%msg%n</pattern>
            </encoder>
        </appender>
    </sift>

</appender>

Now, I would like to have a RollingFileAppender in there, but only for the messages without context. Those with context are generally not very large, but the global one is.

Is this possible?

Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301

3 Answers3

5

I think the best thing in this situation is to extend the SiftingAppender and override append and throw out anything that does not have the key (instead of using the default).

The quickest way I can think of is to create two appenders and have the events sent to both. Then configure the SiftingWhileRejectingDefaultAppender to use the RollingFileAppender and configure the SiftingRejectAllButDefaultAppender to use the regular FileAppender.

SiftingWhileRejectingDefaultAppender

import ch.qos.logback.classic.sift.SiftingAppender;
import ch.qos.logback.classic.spi.ILoggingEvent;

public class SiftingWhileRejectingDefaultAppender extends SiftingAppender {

    @Override
    protected void append(ILoggingEvent event) {
        String discriminatingValue = this.getDiscriminator().getDiscriminatingValue(event);
        if (!discriminatingValue.equals("global")) {
            super.append(event);
        }
    }
}

SiftingRejectAllButDefaultAppender

import ch.qos.logback.classic.sift.SiftingAppender;
import ch.qos.logback.classic.spi.ILoggingEvent;

public class SiftingRejectAllButDefaultAppender extends SiftingAppender {

    @Override
    protected void append(ILoggingEvent event) {
        String discriminatingValue = this.getDiscriminator().getDiscriminatingValue(event);
        if (discriminatingValue.equals("global")) {
            super.append(event);
        }
    }
}
Wesley Womack
  • 308
  • 2
  • 11
3

i think you can do this with the groovy configuration if it is an option for you. You can write the if statement based on the discriminator

appender("SIFT", GSiftingAppender) {
 discriminator(MDCBasedDiscriminator) {
      key = "context"
      defaultValue = "global"
 }
 sift {
  if(..) {
         appender(....) {
         }
  }
 }
}
lorthos
  • 437
  • 3
  • 9
  • If tried this using XML config, and it doesn't work. I get "No nested appenders found within the element in SiftingAppender." as it somehow does't like the 'if'. – David Roussel Apr 10 '13 at 15:34
0

You can use a filter on the appenders. The filter on your appender will include and the filter on the other appenders will exclude