TimeBasedRollingPolicy以编程方式进行回溯

时间:2017-09-17 10:27:07

标签: java logging weblogic logback

我想以编程方式配置logback TimeBasedRollingPolicy,但它不会在路由文件夹中创建我的日志文件。 这是我的代码,它调用记录器创建者类:

    CreateLogger LoggerClass=new CreateLogger();
        private final Logger logger = loggerClass.createLoggerFor("foo", "D:\\logback/testLogback.log");
logger.debug("11111");

D:\ logback / testLogback.log是主日志文件。 这是LoggerCreator类:

public class CreateLogger {
    public Logger createLoggerFor(String string, String file) {
        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 FileAppender<ILoggingEvent>();
    fileAppender.setFile(file);
    fileAppender.setEncoder(ple);
    fileAppender.setContext(lc);
    fileAppender.start();

    RollingFileAppender logFileAppender = new RollingFileAppender();
    logFileAppender.setContext(lc);
    logFileAppender.setName("debug");
    logFileAppender.setEncoder(ple);
    logFileAppender.setAppend(true);
    logFileAppender.setFile(file);
    TimeBasedRollingPolicy logFilePolicy = new TimeBasedRollingPolicy();
    logFilePolicy.setContext(lc);
    logFilePolicy.setParent(logFileAppender);
    logFilePolicy.setFileNamePattern("D:\\logback/archived/testLogBack.%d{yyyy-MM-dd}.%i.log");
    logFilePolicy.setMaxHistory(7);
    logFilePolicy.start();

    logFileAppender.setRollingPolicy(logFilePolicy);
    logFileAppender.start();
    Logger logger = (Logger) LoggerFactory.getLogger(string);
    logger.addAppender(fileAppender);
    logger.setLevel(Level.DEBUG);
    logger.setAdditive(false); 


    logger.addAppender(logFileAppender);

    return logger;
}

}

归档文件应使用提到的模式名称在此路径中: 。d:\的logback /存档/ testLogBack%d {YYYY-MM-DD}%i.log&#34;

我的问题是创建的日志被添加到主日志文件中,而不是基于日期分开。

我也可以在控制台中看到这些行:

14:44:16,864 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@15:83 - no applicable action for [rollingPolicy], current ElementPath  is [[configuration][appender][rollingPolicy]]
14:44:16,864 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@17:30 - no applicable action for [fileNamePattern], current ElementPath  is [[configuration][appender][rollingPolicy][fileNamePattern]]

我的应用服务器是weblogic 12c

1 个答案:

答案 0 :(得分:2)

我自己解决了以下问题:

PatternLayoutEncoder logEncoder = new PatternLayoutEncoder();
            logEncoder.setContext(logCtx);
            logEncoder.setPattern("%-12date{YYYY-MM-dd HH:mm:ss.SSS} %-5level - %msg%n");
            logEncoder.start();

            ConsoleAppender logConsoleAppender = new ConsoleAppender();
            logConsoleAppender.setContext(logCtx);
            logConsoleAppender.setName("console");
            logConsoleAppender.setEncoder(logEncoder);
            logConsoleAppender.start();

            logEncoder = new PatternLayoutEncoder();
            logEncoder.setContext(logCtx);
            logEncoder.setPattern("%-12date{YYYY-MM-dd HH:mm:ss.SSS} %-5level - %msg%n");
            logEncoder.start();

            RollingFileAppender logFileAppender = new RollingFileAppender();
            logFileAppender.setContext(logCtx);
            logFileAppender.setName("logFile");
            logFileAppender.setEncoder(logEncoder);
            logFileAppender.setAppend(true);
            logFileAppender.setFile("logs/logfile.log");

            TimeBasedRollingPolicy logFilePolicy = new TimeBasedRollingPolicy();
            logFilePolicy.setContext(logCtx);
            logFilePolicy.setParent(logFileAppender);
            logFilePolicy.setFileNamePattern("logs/archived/logfile-%d{yyyy-MM-dd_HH}.log.zip");
            logFilePolicy.setMaxHistory(7);
            logFilePolicy.start();

            logFileAppender.setRollingPolicy(logFilePolicy);
            logFileAppender.start();

            Logger log = logCtx.getLogger("Main");
            log.setAdditive(false);
            log.setLevel(Level.INFO);
            log.addAppender(logConsoleAppender);
            log.addAppender(logFileAppender);

            return log;

我还在weblogic-application.xml中激活了prefer-application-packages和prefer-application-resources。

希望它能帮助别人