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.appender.rolling.FileSize; 028import org.apache.logging.log4j.core.config.plugins.Plugin; 029import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 030import org.apache.logging.log4j.core.config.plugins.PluginElement; 031import org.apache.logging.log4j.core.config.plugins.PluginFactory; 032import org.apache.logging.log4j.status.StatusLogger; 033 034/** 035 * PathCondition that accepts paths after the accumulated file size threshold is exceeded during the file tree walk. 036 */ 037@Plugin(name = "IfAccumulatedFileSize", category = Core.CATEGORY_NAME, printObject = true) 038public final class IfAccumulatedFileSize implements PathCondition { 039 private static final Logger LOGGER = StatusLogger.getLogger(); 040 private final long thresholdBytes; 041 private long accumulatedSize; 042 private final PathCondition[] nestedConditions; 043 044 private IfAccumulatedFileSize(final long thresholdSize, final PathCondition[] nestedConditions) { 045 if (thresholdSize <= 0) { 046 throw new IllegalArgumentException("Count must be a positive integer but was " + thresholdSize); 047 } 048 this.thresholdBytes = thresholdSize; 049 this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions, 050 nestedConditions.length); 051 } 052 053 public long getThresholdBytes() { 054 return thresholdBytes; 055 } 056 057 public List<PathCondition> getNestedConditions() { 058 return Collections.unmodifiableList(Arrays.asList(nestedConditions)); 059 } 060 061 /* 062 * (non-Javadoc) 063 * 064 * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path, 065 * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes) 066 */ 067 @Override 068 public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) { 069 accumulatedSize += attrs.size(); 070 final boolean result = accumulatedSize > thresholdBytes; 071 final String match = result ? ">" : "<="; 072 final String accept = result ? "ACCEPTED" : "REJECTED"; 073 LOGGER.trace("IfAccumulatedFileSize {}: {} accumulated size '{}' {} thresholdBytes '{}'", accept, relativePath, 074 accumulatedSize, match, thresholdBytes); 075 if (result) { 076 return IfAll.accept(nestedConditions, basePath, relativePath, attrs); 077 } 078 return result; 079 } 080 081 /* 082 * (non-Javadoc) 083 * 084 * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk() 085 */ 086 @Override 087 public void beforeFileTreeWalk() { 088 accumulatedSize = 0; 089 IfAll.beforeFileTreeWalk(nestedConditions); 090 } 091 092 /** 093 * Create an IfAccumulatedFileSize condition. 094 * 095 * @param threshold The threshold accumulated file size from which files will be deleted. 096 * @return An IfAccumulatedFileSize condition. 097 */ 098 @PluginFactory 099 public static IfAccumulatedFileSize createFileSizeCondition( 100 // @formatter:off 101 @PluginAttribute("exceeds") final String size, 102 @PluginElement("PathConditions") final PathCondition... nestedConditions) { 103 // @formatter:on 104 105 if (size == null) { 106 LOGGER.error("IfAccumulatedFileSize missing mandatory size threshold."); 107 } 108 final long threshold = size == null ? Long.MAX_VALUE : FileSize.parse(size, Long.MAX_VALUE); 109 return new IfAccumulatedFileSize(threshold, nestedConditions); 110 } 111 112 @Override 113 public String toString() { 114 final String nested = nestedConditions.length == 0 ? "" : " AND " + Arrays.toString(nestedConditions); 115 return "IfAccumulatedFileSize(exceeds=" + thresholdBytes + nested + ")"; 116 } 117}