logback以编程方式获取ref appender

时间:2016-02-17 10:44:54

标签: java java-8 logback

拥有此logback文件配置

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>build/test.log</file>
    <append>false</append>
    <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
        <providers>
            <timestamp/>
            <arguments/>
            <version/>
            <loggerName/>
            <logLevel/>
            <logLevelValue/>
            <stackTrace/>
            <message/>
            <threadName/>
            <provider class="com.tesco.utils.CorrelatedProvider"/>
        </providers>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>build/test_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>1MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
        <!-- keep 30 days' worth of history -->
        <maxHistory>30</maxHistory>
    </rollingPolicy>
</appender>

<appender name="ASYNC_FILE" class="ch.qos.logback.classic.AsyncAppender">
    <appender-ref ref="FILE"/>
</appender>

<root level="INFO">
    <appender-ref ref="ASYNC_FILE"/>
</root>

我试图获取appender FILE但是现在唯一能得到的是ASYNC_FILE。如何获取ASYNC_FILE的ref appender?。 这是我到目前为止所做的代码:

  RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<>();
        LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
        for (Logger logger : context.getLoggerList()) {
            for (Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders(); index.hasNext();) {
                Appender<ILoggingEvent> appender = index.next();
                if(appender.getName().equals("ASYNC_FILE")){
                    //How can I get the reference appender FILE?
                    rollingFileAppender = (RollingFileAppender<ILoggingEvent>) appender;
                }
            }
        }

我忘了将appender转换为AsyncAppender,这就是为什么subAppenders没有暴露

   RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<>();
        LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
        for (Logger logger : context.getLoggerList()) {
            for (Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders(); index.hasNext();) {
                Appender<ILoggingEvent> appender = index.next();
                if(appender.getName().equals("ASYNC_TIMING_LOG")){
                    AsyncAppender asyncAppender = (AsyncAppender) appender;
                    rollingFileAppender = (RollingFileAppender<ILoggingEvent>) asyncAppender.getAppender("TIMING_LOG");
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

我忘了将appender转换为AsyncAppender,这就是为什么subAppenders没有暴露

   RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<>();
    LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
    for (Logger logger : context.getLoggerList()) {
        for (Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders(); index.hasNext();) {
            Appender<ILoggingEvent> appender = index.next();
            if(appender.getName().equals("ASYNC_TIMING_LOG")){
                AsyncAppender asyncAppender = (AsyncAppender) appender;
                rollingFileAppender = (RollingFileAppender<ILoggingEvent>) asyncAppender.getAppender("TIMING_LOG");
            }
        }
    }