Class DeleteAction
- java.lang.Object
-
- org.apache.logging.log4j.core.appender.rolling.action.AbstractAction
-
- org.apache.logging.log4j.core.appender.rolling.action.AbstractPathAction
-
- org.apache.logging.log4j.core.appender.rolling.action.DeleteAction
-
- All Implemented Interfaces:
java.lang.Runnable
,Action
@Plugin(name="Delete", category="Core", printObject=true) public class DeleteAction extends AbstractPathAction
Rollover or scheduled action for deleting old log files that are accepted by the specified PathFilters.
-
-
Field Summary
Fields Modifier and Type Field Description private PathSorter
pathSorter
private ScriptCondition
scriptCondition
private boolean
testMode
-
Fields inherited from class org.apache.logging.log4j.core.appender.rolling.action.AbstractAction
LOGGER
-
-
Constructor Summary
Constructors Constructor Description DeleteAction(java.lang.String basePath, boolean followSymbolicLinks, int maxDepth, boolean testMode, PathSorter sorter, PathCondition[] pathConditions, ScriptCondition scriptCondition, StrSubstitutor subst)
Creates a new DeleteAction that starts scanning for files to delete from the specified base path.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.util.List<PathWithAttributes>
callScript()
static DeleteAction
createDeleteAction(java.lang.String basePath, boolean followLinks, int maxDepth, boolean testMode, PathSorter sorterParameter, PathCondition[] pathConditions, ScriptCondition scriptCondition, Configuration config)
Create a DeleteAction.protected java.nio.file.FileVisitor<java.nio.file.Path>
createFileVisitor(java.nio.file.Path visitorBaseDir, java.util.List<PathCondition> conditions)
Creates a newFileVisitor<Path>
to pass to theFiles.walkFileTree(Path, Set, int, FileVisitor)
method when theAbstractPathAction.execute()
method is invoked.protected void
delete(java.nio.file.Path path)
Deletes the specified file.private void
deleteSelectedFiles(java.util.List<PathWithAttributes> selectedForDeletion)
boolean
execute()
Performs action.boolean
execute(java.nio.file.FileVisitor<java.nio.file.Path> visitor)
private boolean
executeScript()
(package private) java.util.List<PathWithAttributes>
getSortedPaths()
Returns a sorted list of all files up to maxDepth under the basePath.boolean
isTestMode()
Returnstrue
if files are not deleted even when all conditions accept a path,false
otherwise.private void
trace(java.lang.String label, java.util.List<PathWithAttributes> sortedPaths)
-
Methods inherited from class org.apache.logging.log4j.core.appender.rolling.action.AbstractPathAction
getBasePath, getBasePathString, getMaxDepth, getOptions, getPathConditions, getStrSubstitutor, isFollowSymbolicLinks, toString
-
Methods inherited from class org.apache.logging.log4j.core.appender.rolling.action.AbstractAction
close, isComplete, isInterrupted, reportException, run
-
-
-
-
Field Detail
-
pathSorter
private final PathSorter pathSorter
-
testMode
private final boolean testMode
-
scriptCondition
private final ScriptCondition scriptCondition
-
-
Constructor Detail
-
DeleteAction
DeleteAction(java.lang.String basePath, boolean followSymbolicLinks, int maxDepth, boolean testMode, PathSorter sorter, PathCondition[] pathConditions, ScriptCondition scriptCondition, StrSubstitutor subst)
Creates a new DeleteAction that starts scanning for files to delete from the specified base path.- Parameters:
basePath
- base path from where to start scanning for files to delete.followSymbolicLinks
- whether to follow symbolic links. Default is false.maxDepth
- The maxDepth parameter is the maximum number of levels of directories to visit. A value of 0 means that only the starting file is visited, unless denied by the security manager. A value of MAX_VALUE may be used to indicate that all levels should be visited.testMode
- if true, files are not deleted but instead a message is printed to the status logger at INFO level. Users can use this to do a dry run to test if their configuration works as expected.sorter
- sortspathConditions
- an array of path filters (if more than one, they all need to accept a path before it is deleted).scriptCondition
-
-
-
Method Detail
-
execute
public boolean execute() throws java.io.IOException
Description copied from class:AbstractAction
Performs action.- Specified by:
execute
in interfaceAction
- Overrides:
execute
in classAbstractPathAction
- Returns:
- true if successful.
- Throws:
java.io.IOException
- if IO error.
-
executeScript
private boolean executeScript() throws java.io.IOException
- Throws:
java.io.IOException
-
callScript
private java.util.List<PathWithAttributes> callScript() throws java.io.IOException
- Throws:
java.io.IOException
-
deleteSelectedFiles
private void deleteSelectedFiles(java.util.List<PathWithAttributes> selectedForDeletion) throws java.io.IOException
- Throws:
java.io.IOException
-
delete
protected void delete(java.nio.file.Path path) throws java.io.IOException
Deletes the specified file.- Parameters:
path
- the file to delete- Throws:
java.io.IOException
- if a problem occurred deleting the file
-
execute
public boolean execute(java.nio.file.FileVisitor<java.nio.file.Path> visitor) throws java.io.IOException
- Overrides:
execute
in classAbstractPathAction
- Throws:
java.io.IOException
-
trace
private void trace(java.lang.String label, java.util.List<PathWithAttributes> sortedPaths)
-
getSortedPaths
java.util.List<PathWithAttributes> getSortedPaths() throws java.io.IOException
Returns a sorted list of all files up to maxDepth under the basePath.- Returns:
- a sorted list of files
- Throws:
java.io.IOException
-
isTestMode
public boolean isTestMode()
Returnstrue
if files are not deleted even when all conditions accept a path,false
otherwise.- Returns:
true
if files are not deleted even when all conditions accept a path,false
otherwise
-
createFileVisitor
protected java.nio.file.FileVisitor<java.nio.file.Path> createFileVisitor(java.nio.file.Path visitorBaseDir, java.util.List<PathCondition> conditions)
Description copied from class:AbstractPathAction
Creates a newFileVisitor<Path>
to pass to theFiles.walkFileTree(Path, Set, int, FileVisitor)
method when theAbstractPathAction.execute()
method is invoked.The visitor is responsible for processing the files it encounters that are accepted by all filters.
- Specified by:
createFileVisitor
in classAbstractPathAction
- Parameters:
visitorBaseDir
- base dir from where to start scanning for files to processconditions
- filters that determine if a file should be processed- Returns:
- a new
FileVisitor<Path>
-
createDeleteAction
@PluginFactory public static DeleteAction createDeleteAction(@PluginAttribute("basePath") java.lang.String basePath, @PluginAttribute("followLinks") boolean followLinks, @PluginAttribute(value="maxDepth",defaultInt=1) int maxDepth, @PluginAttribute("testMode") boolean testMode, @PluginElement("PathSorter") PathSorter sorterParameter, @PluginElement("PathConditions") PathCondition[] pathConditions, @PluginElement("ScriptCondition") ScriptCondition scriptCondition, @PluginConfiguration Configuration config)
Create a DeleteAction.- Parameters:
basePath
- base path from where to start scanning for files to delete.followLinks
- whether to follow symbolic links. Default is false.maxDepth
- The maxDepth parameter is the maximum number of levels of directories to visit. A value of 0 means that only the starting file is visited, unless denied by the security manager. A value of MAX_VALUE may be used to indicate that all levels should be visited.testMode
- if true, files are not deleted but instead a message is printed to the status logger at INFO level. Users can use this to do a dry run to test if their configuration works as expected. Default is false.PathSorter
- a plugin implementing thePathSorter
interfacePathConditions
- an array of path conditions (if more than one, they all need to accept a path before it is deleted).config
- The Configuration.- Returns:
- A DeleteAction.
-
-