001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.appender.rolling.action; 018 019import java.nio.file.Path; 020import java.nio.file.attribute.BasicFileAttributes; 021import java.util.Arrays; 022import java.util.Collections; 023import java.util.List; 024 025import org.apache.logging.log4j.Logger; 026import org.apache.logging.log4j.core.Core; 027import org.apache.logging.log4j.core.config.plugins.Plugin; 028import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 029import org.apache.logging.log4j.core.config.plugins.PluginElement; 030import org.apache.logging.log4j.core.config.plugins.PluginFactory; 031import org.apache.logging.log4j.status.StatusLogger; 032 033/** 034 * PathCondition that accepts paths after some count threshold is exceeded during the file tree walk. 035 */ 036@Plugin(name = "IfAccumulatedFileCount", category = Core.CATEGORY_NAME, printObject = true) 037public final class IfAccumulatedFileCount implements PathCondition { 038 private static final Logger LOGGER = StatusLogger.getLogger(); 039 private final int threshold; 040 private int count; 041 private final PathCondition[] nestedConditions; 042 043 private IfAccumulatedFileCount(final int thresholdParam, final PathCondition[] nestedConditions) { 044 if (thresholdParam <= 0) { 045 throw new IllegalArgumentException("Count must be a positive integer but was " + thresholdParam); 046 } 047 this.threshold = thresholdParam; 048 this.nestedConditions = PathCondition.copy(nestedConditions); 049 } 050 051 public int getThresholdCount() { 052 return threshold; 053 } 054 055 public List<PathCondition> getNestedConditions() { 056 return Collections.unmodifiableList(Arrays.asList(nestedConditions)); 057 } 058 059 /* 060 * (non-Javadoc) 061 * 062 * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path, 063 * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes) 064 */ 065 @Override 066 public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) { 067 final boolean result = ++count > threshold; 068 final String match = result ? ">" : "<="; 069 final String accept = result ? "ACCEPTED" : "REJECTED"; 070 LOGGER.trace("IfAccumulatedFileCount {}: {} count '{}' {} threshold '{}'", accept, relativePath, count, match, 071 threshold); 072 if (result) { 073 return IfAll.accept(nestedConditions, basePath, relativePath, attrs); 074 } 075 return result; 076 } 077 078 /* 079 * (non-Javadoc) 080 * 081 * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk() 082 */ 083 @Override 084 public void beforeFileTreeWalk() { 085 count = 0; 086 IfAll.beforeFileTreeWalk(nestedConditions); 087 } 088 089 /** 090 * Create an IfAccumulatedFileCount condition. 091 * 092 * @param threshold The threshold count from which files will be deleted. 093 * @return An IfAccumulatedFileCount condition. 094 */ 095 @PluginFactory 096 public static IfAccumulatedFileCount createFileCountCondition( 097 // @formatter:off 098 @PluginAttribute(value = "exceeds", defaultInt = Integer.MAX_VALUE) final int threshold, 099 @PluginElement("PathConditions") final PathCondition... nestedConditions) { 100 // @formatter:on 101 102 if (threshold == Integer.MAX_VALUE) { 103 LOGGER.error("IfAccumulatedFileCount invalid or missing threshold value."); 104 } 105 return new IfAccumulatedFileCount(threshold, nestedConditions); 106 } 107 108 @Override 109 public String toString() { 110 final String nested = nestedConditions.length == 0 ? "" : " AND " + Arrays.toString(nestedConditions); 111 return "IfAccumulatedFileCount(exceeds=" + threshold + nested + ")"; 112 } 113}