private static RollingFileAppender createFileAppender(String name, String logFile, String maxSizeKey, Map<String, Exception> errors) { RollingFileAppender.Builder builder = RollingFileAppender.newBuilder(); builder.setConfiguration(new NullConfiguration()); builder.withName(name); builder.withLayout(getDetailedLayout()); builder.withFileName(logFile); builder.withFilePattern(logFile); builder.withAppend(true); builder.withImmediateFlush(true); int limit = getLimit(maxSizeKey, errors); if (limit == 0) limit = 900 * 1024 * 1024; // Default in Logging.DEFAULT_MAX_FILE_SIZE builder.withPolicy(SizeBasedTriggeringPolicy.createPolicy(Integer.toString(limit))); return builder.build(); }
private void testRollingFileAppender(final String configResource, final String name, final String filePattern) throws URISyntaxException { final Configuration configuration = getConfiguration(configResource); final Appender appender = configuration.getAppender(name); assertNotNull(appender); assertEquals(name, appender.getName()); assertTrue(appender.getClass().getName(), appender instanceof RollingFileAppender); final RollingFileAppender rfa = (RollingFileAppender) appender; assertEquals("target/hadoop.log", rfa.getFileName()); assertEquals(filePattern, rfa.getFilePattern()); final TriggeringPolicy triggeringPolicy = rfa.getTriggeringPolicy(); assertNotNull(triggeringPolicy); assertTrue(triggeringPolicy.getClass().getName(), triggeringPolicy instanceof CompositeTriggeringPolicy); final CompositeTriggeringPolicy ctp = (CompositeTriggeringPolicy) triggeringPolicy; final TriggeringPolicy[] triggeringPolicies = ctp.getTriggeringPolicies(); assertEquals(1, triggeringPolicies.length); final TriggeringPolicy tp = triggeringPolicies[0]; assertTrue(tp.getClass().getName(), tp instanceof SizeBasedTriggeringPolicy); final SizeBasedTriggeringPolicy sbtp = (SizeBasedTriggeringPolicy) tp; assertEquals(256 * 1024 * 1024, sbtp.getMaxFileSize()); final RolloverStrategy rolloverStrategy = rfa.getManager().getRolloverStrategy(); assertTrue(rolloverStrategy.getClass().getName(), rolloverStrategy instanceof DefaultRolloverStrategy); final DefaultRolloverStrategy drs = (DefaultRolloverStrategy) rolloverStrategy; assertEquals(20, drs.getMaxIndex()); configuration.start(); configuration.stop(); }
@SuppressWarnings("deprecation") private RollingFileAppender initLogger(String loggerName, String filePrefix) { String fileName = Paths.get(rootPath, filePrefix + "." + loggerName + ".dat").toString(); String filePattern = Paths.get(rootPath, filePrefix + "." + loggerName + "-%i.dat").toString(); PatternLayout layout = PatternLayout.newBuilder().withPattern(PatternLayout.DEFAULT_CONVERSION_PATTERN).build(); SizeBasedTriggeringPolicy policy = SizeBasedTriggeringPolicy.createPolicy(maxFileSize); DefaultRolloverStrategy strategy = DefaultRolloverStrategy.createStrategy(String.valueOf(maxFileCount), null, null, null, null, false, config); //TODO:use RollingFileAppender.newBuilder throw No such static method exception,will improve later! return RollingFileAppender .createAppender(fileName, filePattern, "true", loggerName, "true", null, "true", policy, strategy, layout, null, null, null, null, config); }