下面列出了怎么用org.apache.logging.log4j.core.appender.rolling.action.DeleteAction的API类实例代码及写法,或者点击链接到github查看源代码。
private static DefaultRolloverStrategy createStrategyByAction(String loggerName, String loggerDir) {
IfFileName ifFileName = IfFileName.createNameCondition(null, loggerName + "\\.\\d{4}-\\d{2}-\\d{2}.*");
IfLastModified ifLastModified = IfLastModified.createAgeCondition(Duration.parse("1d"));
DeleteAction deleteAction = DeleteAction.createDeleteAction(loggerDir, false, 1, false, null,
new PathCondition[] {ifLastModified, ifFileName}, null, config);
Action[] actions = new Action[] {deleteAction};
return DefaultRolloverStrategy.createStrategy("7", "1", null, null, actions, false, config);
}
private static DeleteAction create(final String path, final boolean followLinks, final int maxDepth, final boolean testMode,
final PathCondition[] conditions) {
final Configuration config = new BasicConfigurationFactory().new BasicConfiguration();
final DeleteAction delete = DeleteAction.createDeleteAction(path, followLinks, maxDepth, testMode, null, conditions,
null, config);
return delete;
}
@Test
public void testGetBasePathResolvesLookups() {
final DeleteAction delete = createAnyFilter("${sys:user.home}/a/b/c", false, 1, false);
final Path actual = delete.getBasePath();
final String expected = System.getProperty("user.home") + "/a/b/c";
assertEquals(FileSystems.getDefault().getPath(expected), actual);
}
@Test
public void testGetFiltersReturnsConstructorValue() {
final PathCondition[] filters = {new FixedCondition(true), new FixedCondition(false)};
final DeleteAction delete = create("any", true, 0, false, filters);
assertEquals(Arrays.asList(filters), delete.getPathConditions());
}
@Test
public void testCreateFileVisitorTestModeIsActionTestMode() {
final DeleteAction delete = createAnyFilter("any", true, 0, false);
assertFalse(delete.isTestMode());
final FileVisitor<Path> visitor = delete.createFileVisitor(delete.getBasePath(), delete.getPathConditions());
assertTrue(visitor instanceof DeletingVisitor);
assertFalse(((DeletingVisitor) visitor).isTestMode());
final DeleteAction deleteTestMode = createAnyFilter("any", true, 0, true);
assertTrue(deleteTestMode.isTestMode());
final FileVisitor<Path> testVisitor = deleteTestMode.createFileVisitor(delete.getBasePath(),
delete.getPathConditions());
assertTrue(testVisitor instanceof DeletingVisitor);
assertTrue(((DeletingVisitor) testVisitor).isTestMode());
}
public void init() throws Exception {
String active = (String) this.systemParams.get(INIT_PROP_LOG_ACTIVE_FILE_OUTPUT);
if (StringUtils.isEmpty(active) || !active.equalsIgnoreCase("true")) {
return;
}
String appenderName = "ENTANDO";
String conversionPattern = (String) this.systemParams.get("log4jConversionPattern");
if (StringUtils.isBlank(conversionPattern)) {
conversionPattern = "%d{yyyy-MM-dd HH:mm:ss.SSS} - %-5p - %c - %m%n";
}
String maxFileSize = (String) this.systemParams.get(INIT_PROP_LOG_FILE_SIZE);
if (StringUtils.isBlank(maxFileSize)) {
maxFileSize = "1MB"; //default size
} else {
long mega = new Long(maxFileSize) / KILOBYTE;
maxFileSize = mega + "KB";
}
String filePattern = (String) this.systemParams.get(INIT_PROP_LOG_FILE_PATTERN);
String filename = (String) this.systemParams.get(INIT_PROP_LOG_NAME);
int maxBackupIndex = Integer.parseInt((String) this.systemParams.get(INIT_PROP_LOG_FILES_COUNT));
String log4jLevelString = (String) this.systemParams.get(INIT_PROP_LOG_LEVEL);
if (StringUtils.isBlank(log4jLevelString)) {
log4jLevelString = "INFO"; //default level
}
Configurator.setRootLevel(Level.getLevel(log4jLevelString));
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
loggerContext.getRootLogger().setLevel(Level.getLevel(log4jLevelString));
Configurator.setAllLevels(loggerContext.getRootLogger().getName(), Level.getLevel(log4jLevelString));
Configuration configuration = loggerContext.getConfiguration();
RollingFileAppender fileAppender = (RollingFileAppender) configuration.getAppender(appenderName);
if (null == fileAppender) {
PathCondition[] pathConditions = new PathCondition[]{IfAccumulatedFileCount.createFileCountCondition(maxBackupIndex)};
String basePath = filePattern.substring(0, filePattern.lastIndexOf(File.separator));
DeleteAction deleteAction = DeleteAction.createDeleteAction(basePath, true, 1, false, null, pathConditions, null, configuration);
SizeBasedTriggeringPolicy policy = SizeBasedTriggeringPolicy.createPolicy(maxFileSize);
PatternLayout layout = PatternLayout.newBuilder().withPattern(conversionPattern).build();
DefaultRolloverStrategy strategy = DefaultRolloverStrategy.newBuilder()
.withConfig(configuration).withMax(String.valueOf(maxBackupIndex))
.withCustomActions(new Action[]{deleteAction}).build();
fileAppender = RollingFileAppender.newBuilder()
.withName(appenderName)
.setConfiguration(configuration)
.withLayout(layout)
.withFileName(filename)
.withFilePattern(filePattern)
.withPolicy(policy)
.withStrategy(strategy)
.build();
configuration.addAppender(fileAppender);
Configurator.setLevel(appenderName, Level.getLevel(log4jLevelString));
fileAppender.start();
}
AsyncAppender async = (AsyncAppender) loggerContext.getRootLogger().getAppenders().get("async");
if (null == async) {
AppenderRef ref = AppenderRef.createAppenderRef(appenderName, Level.getLevel(log4jLevelString), null);
async = AsyncAppender.newBuilder().setName("async")
.setConfiguration(configuration)
.setAppenderRefs(new AppenderRef[]{ref}).build();
configuration.addAppender(async);
loggerContext.getRootLogger().addAppender(async);
async.start();
}
loggerContext.updateLoggers();
}
private static DeleteAction createAnyFilter(final String path, final boolean followLinks, final int maxDepth, final boolean testMode) {
final PathCondition[] pathFilters = {new FixedCondition(true)};
return create(path, followLinks, maxDepth, testMode, pathFilters);
}
@Test
public void testGetBasePathStringReturnsOriginalParam() {
final DeleteAction delete = createAnyFilter("${sys:user.home}/a/b/c", false, 1, false);
assertEquals("${sys:user.home}/a/b/c", delete.getBasePathString());
}
@Test
public void testGetMaxDepthReturnsConstructorValue() {
final DeleteAction delete = createAnyFilter("any", false, 23, false);
assertEquals(23, delete.getMaxDepth());
}
@Test
public void testGetOptionsReturnsEmptySetIfNotFollowingLinks() {
final DeleteAction delete = createAnyFilter("any", false, 0, false);
assertEquals(Collections.emptySet(), delete.getOptions());
}
@Test
public void testGetOptionsReturnsSetWithFollowLinksIfFollowingLinks() {
final DeleteAction delete = createAnyFilter("any", true, 0, false);
assertEquals(EnumSet.of(FileVisitOption.FOLLOW_LINKS), delete.getOptions());
}
@Test
public void testCreateFileVisitorReturnsDeletingVisitor() {
final DeleteAction delete = createAnyFilter("any", true, 0, false);
final FileVisitor<Path> visitor = delete.createFileVisitor(delete.getBasePath(), delete.getPathConditions());
assertTrue(visitor instanceof DeletingVisitor);
}