Yi Tian
Yi Tian

Reputation: 81

how to use logger while setting SiftingAppender in logback.xml

I have this logback configuration:

<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
    <discriminator class="com.blackHole.programmingTrading.config.ThreadNameBasedDiscriminator"/>
    <sift>
        <appender name="FILE-${threadName}" class="ch.qos.logback.core.FileAppender">
            <file>${LOG_HOME}/app-${threadName}.log</file>
            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>${UNION_LOG_PATTERN}</pattern>
            </layout>
        </appender>
    </sift>
</appender>

and have this configuration class:

public class ThreadNameBasedDiscriminator implements Discriminator<ILoggingEvent> {
    private static final String KEY = "threadName";
    private boolean started;

    @Override
    public String getDiscriminatingValue(ILoggingEvent iLoggingEvent) {
        return iLoggingEvent.getThreadName();
    }

    @Override
    public String getKey() {
        return KEY;
    }

    public void start() {
        started = true;
    }

    public void stop() {
        started = false;
    }

    public boolean isStarted() {
        return started;
    }
}

but when I get logger like this:

LoggerFactory.getLogger(Logger.class)

or this:

LoggerFactory.getLogger("SIFT")

the siftAppender don't work, and I have other appenders working together, so how to deal with it ?

Upvotes: 1

Views: 2795

Answers (1)

bur&#230;quete
bur&#230;quete

Reputation: 14688

With identical settings, just using the logger like below works perfectly;

@Component
public class SomeService {

    public void test() {
        Logger logger = LoggerFactory.getLogger(SomeService.class);
        logger.error("hey");
    }
}

Then I can see the log in app-myThread.log in my local;

14:43:01.808 [myThread]   ERROR com.example.demo.SomeService - hey

If you are trying to get a logger with its name specifically, you'd need to add below in your logback.xml;

<logger name="mylogger" level="error">
    <appender-ref ref="SIFT"/>
</logger>

Because appender != logger, you can add your own configuration to this...

Then you can do;

LoggerFactory.getLogger("mylogger");

will result in a bit different log;

14:53:23.447 [myThread]   ERROR mylogger - hey

Upvotes: 1

Related Questions