logback以编程方式添加appendar

时间:2014-10-06 02:22:31

标签: logback

我想以编程方式登录,但这段代码不正确

这是我的配置代码......

public enum Logger {
    INSTANCE;

    Logger logger;
    LoggerContext loggerContext;

    public final String CONSOLE_APPENDER_NAME="CONSOLE_";
    public final String FILE_APPENDER_NAME="FILE_";
    public final String LOGGER_NAME="LOGGER";
    public final String LOG_FILE_NAME="log.log";

    Logger(){
        loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

        String patternRf = "%d{HH:mm:ss} %-5level [%thread] %logger{36} - %msg%n";
        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setPattern(patternRf);
        encoder.setContext(loggerContext);
        encoder.start();

        ConsoleAppender<ILoggingEvent> soutAppender = new ConsoleAppender<ILoggingEvent>();
        soutAppender.setName(CONSOLE_APPENDER_NAME);
        soutAppender.setEncoder(encoder);

        loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME).detachAndStopAllAppenders();
        logger = loggerContext.getLogger(LOGGER_NAME);
        logger.detachAndStopAllAppenders();


        IPath logDirPath = new Path("/log");
        File logDir = new File(logDirPath.toOSString());
        if(!logDir.exists()){
            logDir.mkdir();
        }
        RollingFileAppender<ILoggingEvent> rfAppender = rollingFileLog(logDirPath, loggerContext, encoder);

        rfAppender.start();
        soutAppender.start();

        logger.addAppender(soutAppender);
        logger.addAppender(rfAppender);

    }

    private RollingFileAppender<ILoggingEvent> rollingFileLog(IPath logDirPath, LoggerContext loggerContext, PatternLayoutEncoder encoder) {
        RollingFileAppender<ILoggingEvent> logFileAppender = new RollingFileAppender<ILoggingEvent>();
        logFileAppender.setContext(loggerContext);
        logFileAppender.setName(FILE_APPENDER_NAME);
        logFileAppender.setEncoder(encoder);
        logFileAppender.setAppend(true);
        logFileAppender.setFile(logDirPath.append(LOG_FILE_NAME).toOSString());

        TimeBasedRollingPolicy<ILoggingEvent> logFilePolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
        logFilePolicy.setContext(loggerContext);
        logFilePolicy.setParent(logFileAppender);
        logFilePolicy.setFileNamePattern("./log/%d{yyyy,aux}/%d{MM,aux}/%d{dd,aux}/service_rolling.log.%d{yyyy-MM-dd_hhmmss}");
        logFilePolicy.setMaxHistory(7);
        logFilePolicy.start();

        logFileAppender.setRollingPolicy(logFilePolicy);

        return logFileAppender;
    }   

    public void debug(String msg){
        logger.debug(msg);
    }



}

这是我的测试代码

public class test {
    public static void main(String[] args) {
        Logger.INSTANCE.debug("a");
        Logger.INSTANCE.debug("b");
        Logger.INSTANCE.debug("c");
        Logger.INSTANCE.debug("d");
        Logger.INSTANCE.debug("e");
        Logger.INSTANCE.debug("f");
        Logger.INSTANCE.debug("g");
        Logger.INSTANCE.debug("h");

    }
}
测试后我的控制台写了这条消息 11:14:53 DEBUG [Thread-1] LOGGER - a

和我的日志文件写了这条消息

11:14:53 DEBUG [Thread-1] LOGGER - a
11:14:53 DEBUG [Thread-1] LOGGER - b
11:14:53 DEBUG [Thread-1] LOGGER - b
11:14:53 DEBUG [Thread-1] LOGGER - c
11:14:53 DEBUG [Thread-1] LOGGER - c
11:14:53 DEBUG [Thread-1] LOGGER - d
11:14:53 DEBUG [Thread-1] LOGGER - d
11:14:53 DEBUG [Thread-1] LOGGER - e
11:14:53 DEBUG [Thread-1] LOGGER - e
11:14:53 DEBUG [Thread-1] LOGGER - f
.....

我认为第一个日志写入控制台

和其他人写入文件

这个程序的问题是什么..

感谢

1 个答案:

答案 0 :(得分:1)

我无法完全重现这种行为:获取代码(并使其编译)要么将所有内容(两次)写入文件,要么将所有内容(两次)写入控制台,但不是第一个用于控制台的语句和所有后续语句提交。

然而,您的代码的问题是,显然,您无法共享编码器。如果将相同的编码器设置为文件appender和控制台appender,则所有内容都将写入文件或控制台。尝试为您的文件追加器创建第二个Encoder实例(您可以使用相同的配置)。

顺便说一下:而不是

loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME).detachAndStopAllAppenders();

您也可以使用

loggerContext.reset();
我认为,这应该具有相同的效果。