LOG4J2-435 integration test for Delete with 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/53103134 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/53103134 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/53103134
Branch: refs/heads/master Commit: 53103134519cff36ba644f0f9706189953f89c1d Parents: 4ec4a2a Author: rpopma <[email protected]> Authored: Sat Dec 5 19:57:03 2015 +0900 Committer: rpopma <[email protected]> Committed: Sat Dec 5 19:57:03 2015 +0900 ---------------------------------------------------------------------- .../RollingAppenderDeleteScriptTest.java | 85 ++++++++++++++++++++ .../log4j-rolling-with-custom-delete-script.xml | 76 +++++++++++++++++ 2 files changed, 161 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/53103134/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDeleteScriptTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDeleteScriptTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDeleteScriptTest.java new file mode 100644 index 0000000..6f64ebe --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDeleteScriptTest.java @@ -0,0 +1,85 @@ +/* + * 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 RollingAppenderDeleteScriptTest { + + private static final String CONFIG = "log4j-rolling-with-custom-delete-script.xml"; + private static final String DIR = "target/rolling-with-delete-script/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 Logger logger = ctx.getLogger(); + // Trigger the rollover + for (int i = 0; i < 10; ++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 + + final File dir = new File(DIR); + assertTrue("Dir " + DIR + " should exist", dir.exists()); + assertTrue("Dir " + DIR + " should contain files", dir.listFiles().length > 0); + + 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 strIndex = file.getName().substring(5, file.getName().indexOf('.')); + int index = Integer.parseInt(strIndex); + assertTrue(file + " should have odd index", index % 2 == 1); + } + } + + 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/53103134/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 new file mode 100644 index 0000000..ed6ba7a --- /dev/null +++ b/log4j-core/src/test/resources/log4j-rolling-with-custom-delete-script.xml @@ -0,0 +1,76 @@ +<?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/</Property> + </Properties> + + <Appenders> + <RollingFile name="RollingFile" fileName="${base}/rollingtest.log" + filePattern="${base}/test/test-%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 pattern = ~/test-(\d*)\.log/; + assert pattern.getClass() == java.util.regex.Pattern; + + def copy = pathList.collect{it}; + pathList.each { pathWithAttributes -> + def relative = basePath.relativize pathWithAttributes.path + def str = relative.toString(); + LOGGER.trace 'SCRIPT: relative ' + str + " (base=$basePath)"; + + // keep only files with odd indexes + + def m = pattern.matcher(str); + if (m.find()) { + def index = m.group(1) as int; + 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; + } + } + } + LOGGER.trace 'SCRIPT: returning' + copy; + copy; + ]]> + </Script> + </ScriptCondition> + </Delete> + </DefaultRolloverStrategy> + </RollingFile> + </Appenders> + + <Loggers> + <Root level="trace"> + <AppenderRef ref="RollingFile" /> + </Root> + </Loggers> + +</Configuration> \ No newline at end of file
