【问题标题】:RollingFileAppender doesn't create the log fileRollingFileAppender doesn\'t create the log file
【发布时间】:2022-12-01 19:27:43
【问题描述】:

When i use RollingFileAppender, it doesn't create the log file, but if I use FileAppender it creates the log file. with fileAppender = new RollingFileAppender(); no log file is created. with fileAppender = new FileAppender(); log file is created.

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.FileAppender;
import ch.qos.logback.core.rolling.RollingFileAppender;

public class LoggerUtils {
    private static final String FILE_APPENDER = "FILE_APPENDER";
    public static void main(String[] args) throws InterruptedException {
        Logger foo = createLoggerFor("foo");
        Logger bar = createLoggerFor("bar");
        foo.info("this is from foo log");
        bar.info("this is from bar log");
    }

    private static Logger createLoggerFor(String string) {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        
        PatternLayoutEncoder ple = new PatternLayoutEncoder();
        ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
        ple.setContext(lc);
        ple.start();
        FileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
        fileAppender.setFile("test_two.log");
        fileAppender.setEncoder(ple);
        fileAppender.setContext(lc);
        fileAppender.start();

        Logger logger = (Logger) LoggerFactory.getLogger(string);
        logger.addAppender(fileAppender);
        logger.setLevel(Level.DEBUG);
        logger.setAdditive(false);
        logger.warn("Testing....");

        return logger;
    }

}

【问题讨论】:

    标签: java log4j slf4j


    【解决方案1】:

    For RollingFileAppender you must specify both a RollingPolicy and a TriggeringPolicy. Here is the quote from that link:

    To be of any use, a RollingFileAppender must have both a RollingPolicy and a TriggeringPolicy set up.

    Also

    However, if its RollingPolicy also implements the TriggeringPolicy interface, then only the former needs to be specified explicitly.

    And indeed, if you look into the source code of RollingFileAppender here and here you will see that there are "not-null" preconditions on those policies:

    public void start() {
        if (triggeringPolicy == null) {
            addWarn("No TriggeringPolicy was set for the RollingFileAppender named " + getName());
            addWarn(MORE_INFO_PREFIX + RFA_NO_TP_URL);
            return; // BOOM
        }
        ...
        if (rollingPolicy == null) {
            addError("No RollingPolicy was set for the RollingFileAppender named " + getName());
            addError(MORE_INFO_PREFIX + RFA_NO_RP_URL);
            return; // BOOM
        }
        ...
    }
    

    So, to remediate the situation your relevant piece of code can be like the below if you use the standard TimeBasedRollingPolicy which is:

    TimeBasedRollingPolicy is possibly the most popular rolling policy. It defines a rollover policy based on time, for example by day or by month.

    RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<>();
    fileAppender.setFile("test_two.log");
    fileAppender.setEncoder(ple);
    fileAppender.setContext(lc);
    
    TimeBasedRollingPolicy<ILoggingEvent> logFilePolicy = new TimeBasedRollingPolicy<>();
    logFilePolicy.setContext(lc);
    logFilePolicy.setParent(fileAppender);
    logFilePolicy.setFileNamePattern("test_two.log.%d{yyyy-MM-dd_HH}");
    logFilePolicy.setMaxHistory(7);
    logFilePolicy.start();
    
    fileAppender.setRollingPolicy(logFilePolicy);
    fileAppender.start();
    

    Using this code I can see the configured test_two.log file being always created.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-27
      • 2022-12-01
      • 2019-03-09
      • 2022-12-02
      • 2022-12-01
      • 2022-12-27
      • 1970-01-01
      • 2022-12-02
      相关资源
      最近更新 更多