Repository: logging-log4j2 Updated Branches: refs/heads/master 83b35cdaa -> be857478c
LOG4J2-435 added unit test; fixed 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/be857478 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/be857478 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/be857478 Branch: refs/heads/master Commit: be857478c5459968dea1122dfac7172f828e0d47 Parents: 83b35cd Author: rpopma <[email protected]> Authored: Sat Dec 5 23:14:22 2015 +0900 Committer: rpopma <[email protected]> Committed: Sat Dec 5 23:14:22 2015 +0900 ---------------------------------------------------------------------- .../RollingAppenderDeleteScriptFri13thTest.java | 91 ++++++++++++++++++++ ...olling-with-custom-delete-script-fri13th.xml | 77 +++++++++++++++++ src/site/xdoc/manual/appenders.xml | 23 +++-- 3 files changed, 178 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/be857478/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDeleteScriptFri13thTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDeleteScriptFri13thTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDeleteScriptFri13thTest.java new file mode 100644 index 0000000..2cd218e --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDeleteScriptFri13thTest.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.core.appender.rolling; + +import java.io.File; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExternalResource; +import org.junit.rules.RuleChain; + +import static org.junit.Assert.*; + +/** + * + */ +public class RollingAppenderDeleteScriptFri13thTest { + + private static final String CONFIG = "log4j-rolling-with-custom-delete-script-fri13th.xml"; + private static final String DIR = "target/rolling-with-delete-script-fri13th/test"; + + private final LoggerContextRule ctx = new LoggerContextRule(CONFIG); + + @Rule + public RuleChain chain = RuleChain.outerRule(new ExternalResource() { + @Override + protected void before() throws Throwable { + deleteDir(); + } + }).around(ctx); + + @Test + public void testAppender() throws Exception { + final File dir = new File(DIR); + dir.mkdirs(); + for (int i = 1; i <= 30; i++) { + String day = i < 10 ? "0" + i : "" + i; + new File(dir, "test-201511" + day + "-0.log").createNewFile(); + } + assertEquals("Dir " + DIR + " filecount", 30, dir.listFiles().length); + + final Logger logger = ctx.getLogger(); + // Trigger the rollover + for (int i = 0; i < 2; ++i) { + // 30 chars per message: each message triggers a rollover + logger.debug("This is a test message number " + i); // 30 chars: + } + Thread.sleep(100); // Allow time for rollover to complete + + assertTrue("Dir " + DIR + " should exist", dir.exists()); + assertTrue("Dir " + DIR + " filecount=" + dir.listFiles().length, dir.listFiles().length >= 30); + + final File[] files = dir.listFiles(); + for (File file : files) { + System.out.println(file); + } + for (File file : files) { + assertTrue(file.getName() + " starts with 'test-'", file.getName().startsWith("test-")); + assertTrue(file.getName() + " ends with '.log'", file.getName().endsWith(".log")); + String strDate = file.getName().substring(5, 13); + assertFalse(file + " is not Fri 13th", strDate.endsWith("13")); + } + } + + private static void deleteDir() { + final File dir = new File(DIR); + if (dir.exists()) { + final File[] files = dir.listFiles(); + for (final File file : files) { + file.delete(); + } + dir.delete(); + } + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/be857478/log4j-core/src/test/resources/log4j-rolling-with-custom-delete-script-fri13th.xml ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/resources/log4j-rolling-with-custom-delete-script-fri13th.xml b/log4j-core/src/test/resources/log4j-rolling-with-custom-delete-script-fri13th.xml new file mode 100644 index 0000000..e71d1b6 --- /dev/null +++ b/log4j-core/src/test/resources/log4j-rolling-with-custom-delete-script-fri13th.xml @@ -0,0 +1,77 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<Configuration status="TRACE" name="RollingWithCustomDeleteScriptTest"> + <Properties> + <Property name="base">target/rolling-with-delete-script-fri13th/</Property> + </Properties> + + <Appenders> + <RollingFile name="RollingFile" fileName="${base}/rollingtest.log" + filePattern="${base}/test/test-%d{yyyyMMdd}-%i.log"> + <PatternLayout> + <Pattern>%d %p %c{1.} [%t] %m%n</Pattern> + </PatternLayout> + <Policies> + <SizeBasedTriggeringPolicy size="50" /> + </Policies> + <DefaultRolloverStrategy max="100" stopCustomActionsOnError="true"> + <Delete basePath="${base}/test" maxDepth="1" followLinks="false"> + <ScriptCondition> + <Script name="GroovyCondition" language="groovy"><![CDATA[ + import java.nio.file.*; + + def result = []; + def pattern = ~/test-(\d*)-(\d*)\.log/; + + pathList.each { pathWithAttributes -> + def relative = basePath.relativize pathWithAttributes.path + def str = relative.toString(); + statusLogger.trace 'SCRIPT: relative path=' + str + " (base=$basePath)"; + + // remove files created on Friday the 13th + + 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) { + result.add pathWithAttributes + statusLogger.trace 'SCRIPT: deleting path ' + pathWithAttributes; + } + } + } + statusLogger.trace 'SCRIPT: returning' + result; + result; + ]]> + </Script> + </ScriptCondition> + </Delete> + </DefaultRolloverStrategy> + </RollingFile> + </Appenders> + + <Loggers> + <Root level="trace"> + <AppenderRef ref="RollingFile" /> + </Root> + </Loggers> + +</Configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/be857478/src/site/xdoc/manual/appenders.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/manual/appenders.xml b/src/site/xdoc/manual/appenders.xml index dea8609..007f5a6 100644 --- a/src/site/xdoc/manual/appenders.xml +++ b/src/site/xdoc/manual/appenders.xml @@ -2817,7 +2817,7 @@ public class JpaLogEntity extends AbstractLogEventWrapperEntity { 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. + The script will delete all rolled over files under the base directory dated Friday the 13th. </p> <pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?> @@ -2836,33 +2836,30 @@ public class JpaLogEntity extends AbstractLogEventWrapperEntity { <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 result = []; 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)"; + statusLogger.trace 'SCRIPT: relative path=' + str + " (base=$basePath)"; - // keep only files with odd indexes + // remove files dated Friday the 13th 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 + 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; + if (friday13th) { + result.add pathWithAttributes; + statusLogger.trace 'SCRIPT: deleting path ' + pathWithAttributes; } } } - statusLogger.trace 'SCRIPT: returning ' + filesToDelete; - filesToDelete; + statusLogger.trace 'SCRIPT: returning ' + result; + result; ]] > <!-- delete space between ]] and > --> </Script> </ScriptCondition>
