有没有办法以编程方式创建RollingFileAppender?

时间:2019-04-10 07:23:03

标签: java logback slf4j jigsaw

我正在尝试为我的项目创建一个记录器和附加器,但是它需要可配置,因此我正在考虑以编程方式对其进行设置。

问题是当我尝试导入“ import ch.qos.logback.core.rolling.RollingFileAppender;”

但是它抛出了以下错误: “类型ch.qos.logback.core.rolling.RollingFileAppender无法访问”

奇怪的是,在我正在使用RollingFileAppender类的行中,建议我进行同样的导入。

我在模块路径中有logback-classic-1.3.0-alpha4.jar和logback-core-1.3.0-alpha4.jar,而module-info.jar是:

module tool.foo {
    requires com.fazecast.jSerialComm;
    requires commons.cli;
    requires java.desktop;
    requires java.xml;
    requires java.naming;
    requires java.net.http;
    requires org.slf4j;
    requires swt;
    requires ch.qos.logback.core;
    requires ch.qos.logback.classic;

    requires static org.junit.jupiter.api;
    requires static org.junit.jupiter.params;
    requires static org.junit.jupiter.engine;

    exports com.enterprise.tools.foo;
}

该类如下:

package com.premium.tools.pamtool.controller;

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.rolling.RollingFileAppender;

public class CsvLogger {

    public static Logger createLoggerFor(String file) {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        PatternLayoutEncoder ple = new PatternLayoutEncoder();

        ple.setPattern("%msg");
        ple.setContext(lc);
        ple.start();
        RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
        fileAppender.setFile(file);
        fileAppender.setEncoder(ple);
        fileAppender.setContext(lc);
        fileAppender.setAppend(false);
        fileAppender.start();

        Logger logger = (Logger) LoggerFactory.getLogger("CsvLogger");
        logger.addAppender(fileAppender);
        logger.setLevel(Level.INFO);
        logger.setAdditive(false); /* set to true if root should log too */
        return logger;
    }

}

0 个答案:

没有答案