org.apache.logging.log4j.core.appender.rolling.action.DeleteAction Java Examples

The following examples show how to use org.apache.logging.log4j.core.appender.rolling.action.DeleteAction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Log4j2Util.java    From summerframework with Apache License 2.0 5 votes vote down vote up
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);
    }
 
Example #2
Source File: DeleteActionTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #3
Source File: DeleteActionTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #4
Source File: DeleteActionTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #5
Source File: DeleteActionTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #6
Source File: ApsSystemUtils.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
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();
}
 
Example #7
Source File: DeleteActionTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example #8
Source File: DeleteActionTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@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());
}
 
Example #9
Source File: DeleteActionTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetMaxDepthReturnsConstructorValue() {
    final DeleteAction delete = createAnyFilter("any", false, 23, false);
    assertEquals(23, delete.getMaxDepth());
}
 
Example #10
Source File: DeleteActionTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetOptionsReturnsEmptySetIfNotFollowingLinks() {
    final DeleteAction delete = createAnyFilter("any", false, 0, false);
    assertEquals(Collections.emptySet(), delete.getOptions());
}
 
Example #11
Source File: DeleteActionTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetOptionsReturnsSetWithFollowLinksIfFollowingLinks() {
    final DeleteAction delete = createAnyFilter("any", true, 0, false);
    assertEquals(EnumSet.of(FileVisitOption.FOLLOW_LINKS), delete.getOptions());
}
 
Example #12
Source File: DeleteActionTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@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);
}