DefaultRolloverStrategy Log4j2不起作用(以编程方式)

时间:2019-02-12 23:38:55

标签: log4j2

我需要创建一个rollingfile附加程序并在运行时使用log4j2设置日志文件的数量。因此,我使用以下代码实现了这一点:

LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();

DefaultRolloverStrategy strategy = DefaultRolloverStrategy.newBuilder()
        .withMax("4")
        .withMin("1")
        .withFileIndex("max")
        .withConfig(config)
        .withCompressionLevelStr(Deflater.NO_COMPRESSION + "")
        .build();

PatternLayout layout = PatternLayout.newBuilder().withConfiguration(config)
        .withPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} [%5p] %pid --- %-40.40logger{39} : %m%n%wEx")
        .build();

RollingFileAppender appender = RollingFileAppender.newBuilder().setConfiguration(config)
        .withName("TraceFileAppender")
        .withLayout(layout)
        .withFileName("log.log")
        .withFilePattern("log.%d{yyyy-MM-ddHHmmSSS}.log")
        .withPolicy(SizeBasedTriggeringPolicy.createPolicy("20KB")
        .withStrategy(strategy)
        .build();

appender.start();
config.addAppender(appender);

LoggerConfig loggerConfig = config.getRootLogger();
loggerConfig.setLevel(Level.toLevel("DEBUG"));
loggerConfig.addAppender(appender, null, null);

除了文件最大数量之外,这一切正常。我的策略中获得的文件数量比4个要多。...怎么了?欢迎任何帮助!

预先感谢

关于彼得

1 个答案:

答案 0 :(得分:0)

pfff花了我一段时间,但我开始工作了……关于此处以编程方式更改log4j2附加程序的信息不多,所以这是我的解决方案:

LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        Configuration config = ctx.getConfiguration();

        PathCondition[] pathConditions = new PathCondition[1];
        pathConditions[0] = IfAccumulatedFileCount.createFileCountCondition(Integer.parseInt(expire));

        DeleteAction action = DeleteAction.createDeleteAction("C:\\logs\\BuddyServer\\", true, 1, false, null, pathConditions, null, config);
        Action[] actions = new Action[1];
        actions[0] = action;

        DefaultRolloverStrategy strategy = DefaultRolloverStrategy.newBuilder()
                .withMax(expire)
                .withCustomActions(actions)
                .withMin("1")
                .withFileIndex("max")
                .withConfig(config)
                .withCompressionLevelStr(Deflater.NO_COMPRESSION + "")  
                .build();


        PatternLayout layout = PatternLayout.newBuilder().withConfiguration(config)
                .withPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} [%5p] %pid --- %-40.40logger{39} : %m%n%wEx")
                .build();

        RollingFileAppender appender = RollingFileAppender.newBuilder().setConfiguration(config)
                .withName("TraceFileAppender")
                .withLayout(layout)
                .withFileName(file + ".log")
                .withFilePattern(file + ".%d{yyyy-MM-ddHHmmSSS}.log")
                .withPolicy(SizeBasedTriggeringPolicy.createPolicy(segment))
                .withStrategy(strategy)
                .build();

        appender.start();
        config.addAppender(appender);

        LoggerConfig loggerConfig = config.getRootLogger();
        loggerConfig.setLevel(Level.toLevel(buddyConfig.getOption("log", "verbose").toUpperCase()));
        loggerConfig.addAppender(appender, null, null);