diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerCreateOnDemandTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerCreateOnDemandTest.java new file mode 100644 index 00000000000..e21494d10a5 --- /dev/null +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerCreateOnDemandTest.java @@ -0,0 +1,72 @@ +/* + * 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 static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.nio.file.Files; +import org.apache.logging.log4j.core.config.NullConfiguration; +import org.apache.logging.log4j.core.layout.PatternLayout; +import org.junit.jupiter.api.Test; + +class RollingFileManagerCreateOnDemandTest { + @Test + void testCreateOnDemandDoesNotCreateDirectoryOrFile() throws Exception { + String logDir = "target/test-logs/onDemand"; + String logFile = logDir + "/test.log"; + File dir = new File(logDir); + File file = new File(logFile); + if (file.exists()) file.delete(); + if (dir.exists()) dir.delete(); + assertFalse(dir.exists(), "Directory should not exist before logging"); + assertFalse(file.exists(), "File should not exist before logging"); + + RollingFileManager manager = RollingFileManager.getFileManager( + logFile, + logFile + ".%d{yyyy-MM-dd}", + true, + false, + NoOpTriggeringPolicy.INSTANCE, + DefaultRolloverStrategy.newBuilder().build(), + null, + PatternLayout.createDefaultLayout(), + 0, + true, + true, + null, + null, + null, + new NullConfiguration()); + assertNotNull(manager); + // Directory and file should still not exist + assertFalse(dir.exists(), "Directory should not exist after manager creation"); + assertFalse(file.exists(), "File should not exist after manager creation"); + + // Log a message + manager.writeToDestination("Hello Log4j2".getBytes(), 0, "Hello Log4j2".length()); + manager.close(); + + // Now directory and file should exist + assertTrue(dir.exists(), "Directory should exist after first log event"); + assertTrue(file.exists(), "File should exist after first log event"); + String content = new String(Files.readAllBytes(file.toPath())); + assertTrue(content.contains("Hello Log4j2")); + } +} 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 afdb7416585..0c62bfac965 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 @@ -310,14 +310,17 @@ public static RollingFileManager getFileManager( if (fileName != null) { file = new File(fileName); - try { - FileUtils.makeParentDirs(file); - final boolean created = createOnDemand ? false : file.createNewFile(); - LOGGER.trace("New file '{}' created = {}", name, created); - } catch (final IOException ioe) { - LOGGER.error("Unable to create file {}", name, ioe); - return null; + if (!createOnDemand) { + try { + FileUtils.makeParentDirs(file); + final boolean created = file.createNewFile(); + LOGGER.trace("New file '{}' created = {}", name, created); + } catch (final IOException ioe) { + LOGGER.error("Unable to create file {}", name, ioe); + return null; + } } + size = append ? file.length() : 0; } @@ -391,12 +394,10 @@ public String getFileName() { @Override protected void createParentDir(File file) { - if (directWrite) { - final File parent = file.getParentFile(); - // If the parent is null the file is in the current working directory. - if (parent != null) { - parent.mkdirs(); - } + try { + FileUtils.makeParentDirs(file); + } catch (IOException e) { + LOGGER.error("Unable to create parent directories for file {}", file, e); } }