LOG4J2-1833 - Log an error message if a file name is specified with the DirectWriteRolloverStrategy
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/915e681e Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/915e681e Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/915e681e Branch: refs/heads/LOG4J2-1431 Commit: 915e681e5e2bc3ad3f2eaeff9d10d38a07ef277c Parents: 14e794b Author: Ralph Goers <[email protected]> Authored: Wed Aug 23 06:27:00 2017 -0700 Committer: Ralph Goers <[email protected]> Committed: Wed Aug 23 06:27:10 2017 -0700 ---------------------------------------------------------------------- .../appender/rolling/RollingFileManager.java | 5 ++ .../rolling/RollingRandomAccessFileManager.java | 4 ++ ...lingAppenderDirectWriteWithFilenameTest.java | 56 ++++++++++++++++++++ ...ndomAppenderDirectWriteWithFilenameTest.java | 55 +++++++++++++++++++ .../src/test/resources/log4j2-random-1833.xml | 24 +++++++++ .../src/test/resources/log4j2-rolling-1833.xml | 24 +++++++++ src/changes/changes.xml | 3 ++ 7 files changed, 171 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/915e681e/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java index 4e07ec2..6ccfe7b 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java @@ -179,6 +179,11 @@ public class RollingFileManager extends FileManager { final boolean immediateFlush, final boolean createOnDemand, final String filePermissions, final String fileOwner, final String fileGroup, final Configuration configuration) { + + if (strategy instanceof DirectWriteRolloverStrategy && fileName != null) { + LOGGER.error("The fileName attribute must not be specified with the DirectWriteRolloverStrategy"); + return null; + } final String name = fileName == null ? pattern : fileName; return narrow(RollingFileManager.class, getManager(name, new FactoryData(fileName, pattern, append, bufferedIO, policy, strategy, advertiseURI, layout, bufferSize, immediateFlush, createOnDemand, http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/915e681e/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java index b203fd2..6d69bd9 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java @@ -101,6 +101,10 @@ public class RollingRandomAccessFileManager extends RollingFileManager { final TriggeringPolicy policy, final RolloverStrategy strategy, final String advertiseURI, final Layout<? extends Serializable> layout, final String filePermissions, final String fileOwner, final String fileGroup, final Configuration configuration) { + if (strategy instanceof DirectWriteRolloverStrategy && fileName != null) { + LOGGER.error("The fileName attribute must not be specified with the DirectWriteRolloverStrategy"); + return null; + } return narrow(RollingRandomAccessFileManager.class, getManager(fileName, new FactoryData(filePattern, isAppend, immediateFlush, bufferSize, policy, strategy, advertiseURI, layout, filePermissions, fileOwner, fileGroup, configuration), FACTORY)); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/915e681e/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteWithFilenameTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteWithFilenameTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteWithFilenameTest.java new file mode 100644 index 0000000..27977f6 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteWithFilenameTest.java @@ -0,0 +1,56 @@ +/* + * 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.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.RuleChain; +import static org.hamcrest.Matchers.hasItemInArray; +import static org.junit.Assert.*; + +/** + * + */ +public class RollingAppenderDirectWriteWithFilenameTest { + + private static final String CONFIG = "log4j2-rolling-1833.xml"; + + private static final String DIR = "target/rolling-1833"; + + public static LoggerContextRule loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG); + + @Rule + public RuleChain chain = loggerContextRule.withCleanFoldersRule(DIR); + + private Logger logger; + + @Before + public void setUp() throws Exception { + this.logger = loggerContextRule.getLogger(RollingAppenderDirectWriteWithFilenameTest.class.getName()); + } + + @Test + public void testAppender() throws Exception { + final File dir = new File(DIR); + assertFalse("Directory created", dir.exists()); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/915e681e/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAppenderDirectWriteWithFilenameTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAppenderDirectWriteWithFilenameTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAppenderDirectWriteWithFilenameTest.java new file mode 100644 index 0000000..b270671 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAppenderDirectWriteWithFilenameTest.java @@ -0,0 +1,55 @@ +/* + * 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.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.RuleChain; +import static org.junit.Assert.assertFalse; + +/** + * + */ +public class RollingRandomAppenderDirectWriteWithFilenameTest { + + private static final String CONFIG = "log4j2-random-1833.xml"; + + private static final String DIR = "target/random-1833"; + + public static LoggerContextRule loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG); + + @Rule + public RuleChain chain = loggerContextRule.withCleanFoldersRule(DIR); + + private Logger logger; + + @Before + public void setUp() throws Exception { + this.logger = loggerContextRule.getLogger(RollingRandomAppenderDirectWriteWithFilenameTest.class.getName()); + } + + @Test + public void testAppender() throws Exception { + final File dir = new File(DIR); + assertFalse("Directory created", dir.exists()); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/915e681e/log4j-core/src/test/resources/log4j2-random-1833.xml ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/resources/log4j2-random-1833.xml b/log4j-core/src/test/resources/log4j2-random-1833.xml new file mode 100644 index 0000000..b72bf4e --- /dev/null +++ b/log4j-core/src/test/resources/log4j2-random-1833.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<Configuration status="WARN" name="MyApp" packages=""> + <Properties> + <Property name="baseDir">target/random-1833</Property> + </Properties> + <Appenders> + <RollingRandomAccessFile name="RollingFile" fileName="${baseDir}/app.log" + filePattern="${baseDir}/app-%d{yyyy-MM-dd'T'HH_mm_ss}.log"> + <PatternLayout pattern="%d %p %c{1.} [%t] %m%n" /> + <TimeBasedTriggeringPolicy interval="10"/> + <DirectWriteRolloverStrategy> + <Delete basePath="${baseDir}"> + <IfFileName glob="app-*.log" /> + <IfAccumulatedFileCount exceeds="10" /> + </Delete> + </DirectWriteRolloverStrategy> + </RollingRandomAccessFile> + </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/915e681e/log4j-core/src/test/resources/log4j2-rolling-1833.xml ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/resources/log4j2-rolling-1833.xml b/log4j-core/src/test/resources/log4j2-rolling-1833.xml new file mode 100644 index 0000000..1ca2f8f --- /dev/null +++ b/log4j-core/src/test/resources/log4j2-rolling-1833.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<Configuration status="WARN" name="MyApp" packages=""> + <Properties> + <Property name="baseDir">target/rolling-1833</Property> + </Properties> + <Appenders> + <RollingFile name="RollingFile" fileName="${baseDir}/app.log" + filePattern="${baseDir}/app-%d{yyyy-MM-dd'T'HH_mm_ss}.log"> + <PatternLayout pattern="%d %p %c{1.} [%t] %m%n" /> + <TimeBasedTriggeringPolicy interval="10"/> + <DirectWriteRolloverStrategy> + <Delete basePath="${baseDir}"> + <IfFileName glob="app-*.log" /> + <IfAccumulatedFileCount exceeds="10" /> + </Delete> + </DirectWriteRolloverStrategy> + </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/915e681e/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 7747d37..566767a 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -31,6 +31,9 @@ - "remove" - Removed --> <release version="2.9.0" date="2017-MM-DD" description="GA Release 2.9.0"> + <action issue="LOG4J2-1833" dev="rgoers" type="fix"> + Prevent NullPointerException when a file name is specified with the DirectWriteRolloverStrategy. + </action> <action issue="LOG4J2-2022" dev="rgoers" type="update"> RFC5424Layout now prints the process id. </action>
