LOG4J2-435 added documentation for ScriptCondition Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/83b35cda Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/83b35cda Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/83b35cda
Branch: refs/heads/master Commit: 83b35cdaa1ff809d0e6e185b176ed70147f5b469 Parents: 1afb925 Author: rpopma <[email protected]> Authored: Sat Dec 5 21:18:16 2015 +0900 Committer: rpopma <[email protected]> Committed: Sat Dec 5 21:18:16 2015 +0900 ---------------------------------------------------------------------- .../rolling/action/ScriptCondition.java | 5 +- .../log4j-rolling-with-custom-delete-script.xml | 6 +- src/site/xdoc/manual/appenders.xml | 155 ++++++++++++++++++- 3 files changed, 154 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/83b35cda/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/ScriptCondition.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/ScriptCondition.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/ScriptCondition.java index c1d4bea..1abb792 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/ScriptCondition.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/ScriptCondition.java @@ -73,8 +73,9 @@ public class ScriptCondition { bindings.put("basePath", basePath); bindings.put("pathList", candidates); bindings.putAll(configuration.getProperties()); + bindings.put("configuration", configuration); bindings.put("substitutor", configuration.getStrSubstitutor()); - bindings.put("LOGGER", LOGGER); + bindings.put("statusLogger", LOGGER); final Object object = configuration.getScriptManager().execute(script.getName(), bindings); return (List<PathWithAttributes>) object; } @@ -92,7 +93,7 @@ public class ScriptCondition { * free to modify and return this list.)</li> * <li>substitutor - a {@link StrSubstitutor} that can be used to look up variables embedded in the base * dir or other properties - * <li>LOGGER - the {@link StatusLogger} that can be used to log events during script execution + * <li>statusLogger - the {@link StatusLogger} that can be used to log events during script execution * <li>any properties declared in the configuration</li> * </ul> * @param configuration the configuration http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/83b35cda/log4j-core/src/test/resources/log4j-rolling-with-custom-delete-script.xml ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/resources/log4j-rolling-with-custom-delete-script.xml b/log4j-core/src/test/resources/log4j-rolling-with-custom-delete-script.xml index ed6ba7a..b94f35d 100644 --- a/log4j-core/src/test/resources/log4j-rolling-with-custom-delete-script.xml +++ b/log4j-core/src/test/resources/log4j-rolling-with-custom-delete-script.xml @@ -43,7 +43,7 @@ pathList.each { pathWithAttributes -> def relative = basePath.relativize pathWithAttributes.path def str = relative.toString(); - LOGGER.trace 'SCRIPT: relative ' + str + " (base=$basePath)"; + statusLogger.trace 'SCRIPT: relative path is ' + str + " (base=$basePath)"; // keep only files with odd indexes @@ -53,11 +53,11 @@ def isOdd = (index % 2) == 1; if (isOdd) { copy.remove pathWithAttributes; // remove path with odd index from list to delete - LOGGER.trace 'SCRIPT: not deleting path ' + pathWithAttributes; + statusLogger.trace 'SCRIPT: not deleting path ' + pathWithAttributes; } } } - LOGGER.trace 'SCRIPT: returning' + copy; + statusLogger.trace 'SCRIPT: returning ' + copy; copy; ]]> </Script> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/83b35cda/src/site/xdoc/manual/appenders.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/manual/appenders.xml b/src/site/xdoc/manual/appenders.xml index cda984d..dea8609 100644 --- a/src/site/xdoc/manual/appenders.xml +++ b/src/site/xdoc/manual/appenders.xml @@ -2527,7 +2527,7 @@ public class JpaLogEntity extends AbstractLogEventWrapperEntity { <tr> <td>pathConditions <a name="DeletePathCondition"/></td> <td>PathCondition[]</td> - <td><p><em>Required.</em> One or more PathCondition elements.</p> + <td><p><em>Required if no ScriptCondition is specified.</em> One or more PathCondition elements.</p> <p> If more than one condition is specified, they all need to accept a path before it is deleted. Conditions can be nested, in which case the @@ -2556,6 +2556,18 @@ public class JpaLogEntity extends AbstractLogEventWrapperEntity { </ul> </td> </tr> + <tr> + <td>scriptCondition <a name="DeleteScriptCondition"/></td> + <td>ScriptCondition</td> + <td><p><em>Required if no PathConditions are specified.</em> A ScriptCondition element specifying a script.</p> + <p> + The ScriptCondition should contain a <a href="#ScriptCondition">Script, + ScriptRef or ScriptFile</a> element that specifies the logic to be executed. + See also the <a href="filters.html#Script">ScriptFilter</a> documentation for more examples of + configuring ScriptFiles and ScriptRefs. + </p> + </td> + </tr> </table> <a name="DeleteIfFileName"/> <table> @@ -2701,9 +2713,10 @@ public class JpaLogEntity extends AbstractLogEventWrapperEntity { triggering policies, will create up to 100 archives on the same day (1-100) that are stored in a directory based on the current year and month, and will compress each archive using gzip and will roll every hour. - Of the old archives under the base directory that match the "*/app-*.log.gz" glob, only the most recent files up to 100 GB are kept, - other files (that match the "*/app-*.log.gz" glob) are deleted. Files that do not match the - "*/app-*.log.gz" glob are not deleted. + + During every rollover, this configuration will delete files that match "*/app-*.log.gz" + and are 30 days old or older, + but keep the most recent 100 GB or the most recent 10 files, whichever comes first. </p> <pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?> @@ -2720,11 +2733,16 @@ public class JpaLogEntity extends AbstractLogEventWrapperEntity { <SizeBasedTriggeringPolicy size="250 MB"/> </Policies> <DefaultRolloverStrategy max="100"> + <!-- + --> <Delete basePath="${baseDir}" maxDepth="2"> <IfFileName glob="*/app-*.log.gz"> - <!-- nested: consider only the size of files that - match the glob pattern, ignore other files --> - <IfAccumulatedFileSize exceeds="100 GB" /> + <IfLastModified age="30d"> + <IfAny> + <IfAccumulatedFileSize exceeds="100 GB" /> + <IfAccumulatedFileCount exceeds="10" /> + </IfAny> + </IfLastModified> </IfFileName> </Delete> </DefaultRolloverStrategy> @@ -2736,7 +2754,130 @@ public class JpaLogEntity extends AbstractLogEventWrapperEntity { </Root> </Loggers> </Configuration>]]></pre> + <a name="ScriptCondition"/> + <table> + <caption align="top">ScriptCondition Parameters</caption> + <tr> + <th>Parameter Name</th> + <th>Type</th> + <th>Description</th> + </tr> + <tr> + <td>script</td> + <td>Script, ScriptFile or ScriptRef</td> + <td>The Script element that specifies the logic to be executed. The script must return a + <tt>java.util.List<<a href="../log4j-core/apidocs/org/apache/logging/log4j/core/appender/rolling/action/PathWithAttributes.html">PathWithAttributes</a>></tt>. + See also the <a href="filters.html#Script">ScriptFilter</a> documentation for an example of + how ScriptFiles and ScriptRefs can be configured. + </td> + </tr> + </table> + <table> + <caption align="top">Script Parameters</caption> + <tr> + <th>Parameter Name</th> + <th>Type</th> + <th>Description</th> + </tr> + <tr> + <td>basePath</td> + <td><tt>java.nio.file.Path</tt></td> + <td>The directory from where the Delete action started scanning for + files to delete. Can be used to relativize the paths in the pathList.</td> + </tr> + <tr> + <td>pathList</td> + <td><tt>java.util.List<<a href="../log4j-core/apidocs/org/apache/logging/log4j/core/appender/rolling/action/PathWithAttributes.html">PathWithAttributes</a>></tt></td> + <td>The list of paths found under the base path up to the specified max depth, + sorted most recently modified files first. + The script is free to modify and return this list.</td> + </tr> + <tr> + <td>statusLogger</td> + <td>StatusLogger</td> + <td>The StatusLogger that can be used to log internal events during script execution.</td> + </tr> + <tr> + <td>configuration</td> + <td>Configuration</td> + <td>The Configuration that owns this ScriptCondition.</td> + </tr> + <tr> + <td>substitutor</td> + <td>StrSubstitutor</td> + <td>The StrSubstitutor used to replace lookup variables.</td> + </tr> + <tr> + <td>?</td> + <td>String</td> + <td>Any properties declared in the configuration.</td> + </tr> + </table> + <p> + Below is a sample configuration that uses a RollingFileAppender with the cron + triggering policy configured to trigger every day at midnight. + Archives are stored in a directory based on the current year and month. + The script will delete all files created on Friday the 13th. + </p> + + <pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?> +<Configuration status="warn" name="MyApp" packages=""> + <Properties> + <Property name="baseDir">logs</Property> + </Properties> + <Appenders> + <RollingFile name="RollingFile" fileName="${baseDir}/app.log" + filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyyMMdd}.log.gz"> + <PatternLayout pattern="%d %p %c{1.} [%t] %m%n" /> + <CronTriggeringPolicy schedule="0 0 0 * * ?"/> + <DefaultRolloverStrategy> + <Delete basePath="${baseDir}" maxDepth="2"> + <ScriptCondition> + <Script name="GroovyCondition" language="groovy"><![CDATA[ + import java.nio.file.*; + + // copy the files that were found under the base path + def filesToDelete = pathList.collect{it}; + + def pattern = ~/\d*\/app-(\d*)\.log\.gz/; + + pathList.each { pathWithAttributes -> + def relative = basePath.relativize pathWithAttributes.path + def str = relative.toString(); + statusLogger.trace 'SCRIPT: relative path is ' + str + " (base=$basePath)"; + + // keep only files with odd indexes + + def m = pattern.matcher(str); + if (m.find()) { + def dateString = m.group(1); + def calendar = Date.parse("yyyyMMdd", dateString).toCalendar(); + def friday13th = calendar.get(Calendar.DAY_OF_MONTH) == 13 + && calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY; + if (!friday13th) { + // returned list (files to delete) should only contain files created on Friday 13th + filesToDelete.remove pathWithAttributes; + statusLogger.trace 'SCRIPT: not deleting path ' + pathWithAttributes; + } + } + } + statusLogger.trace 'SCRIPT: returning ' + filesToDelete; + filesToDelete; + ]] > <!-- delete space between ]] and > --> + </Script> + </ScriptCondition> + </Delete> + </DefaultRolloverStrategy> + </RollingFile> + </Appenders> + <Loggers> + <Root level="error"> + <AppenderRef ref="RollingFile"/> + </Root> + </Loggers> +</Configuration>]]></pre> </subsection> + <a name="RollingRandomAccessFileAppender" /> <subsection name="RollingRandomAccessFileAppender"> <p><i>As of beta-9, the name of this appender has been changed from FastRollingFile to
