This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/2.x by this push:
     new 878f951809 Fix `createOnDemand` for Rolling File Appender (#4072)
878f951809 is described below

commit 878f9518095c0116b12ebc88bc35db07d96113c5
Author: Ramanathan <[email protected]>
AuthorDate: Fri May 22 01:03:29 2026 +0530

    Fix `createOnDemand` for Rolling File Appender (#4072)
    
    Co-authored-by: Piotr P. Karwasz <[email protected]>
    Co-authored-by: Volkan Yazıcı <[email protected]>
---
 .../RollingFileManagerCreateOnDemandTest.java      | 72 ++++++++++++++++++++++
 .../core/appender/rolling/RollingFileManager.java  | 27 ++++----
 ...2006_Fix_RollingFileAppender_createOnDemand.xml | 14 +++++
 3 files changed, 100 insertions(+), 13 deletions(-)

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 0000000000..6a2e16f457
--- /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 java.nio.file.Path;
+import org.apache.logging.log4j.core.config.NullConfiguration;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+class RollingFileManagerCreateOnDemandTest {
+    @Test
+    void testCreateOnDemandDoesNotCreateDirectoryOrFile(@TempDir Path tempDir) 
throws Exception {
+        Path logDir = tempDir.resolve("onDemand");
+        String logFile = logDir.resolve("test.log").toString();
+        File dir = logDir.toFile();
+        File file = new File(logFile);
+        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 afdb741658..0c62bfac96 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 class RollingFileManager extends FileManager {
                             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 class RollingFileManager extends FileManager {
 
     @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);
         }
     }
 
diff --git 
a/src/changelog/.2.x.x/LOG4J2-2006_Fix_RollingFileAppender_createOnDemand.xml 
b/src/changelog/.2.x.x/LOG4J2-2006_Fix_RollingFileAppender_createOnDemand.xml
new file mode 100644
index 0000000000..33d6f9e9fe
--- /dev/null
+++ 
b/src/changelog/.2.x.x/LOG4J2-2006_Fix_RollingFileAppender_createOnDemand.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns="https://logging.apache.org/xml/ns";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="
+           https://logging.apache.org/xml/ns
+           https://logging.apache.org/xml/ns/log4j-changelog-0.xsd";
+       type="fixed">
+  <issue id="2006" 
link="https://github.com/apache/logging-log4j2/issues/2006"/>
+  <issue id="4072" link="https://github.com/apache/logging-log4j2/pull/4072"/>
+  <description format="asciidoc">
+    Fix the `createOnDemand` behavior of `RollingFileAppender` to correctly 
defer file and directory creation
+    until the first log event, while preserving eager creation when disabled.
+  </description>
+</entry>
\ No newline at end of file

Reply via email to