You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2020/07/30 17:42:16 UTC

[logging-log4j2] branch master updated: LOG4J2-2875 - Rollover was failing to create directories when using a DirectFileRolloverStrategy

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

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


The following commit(s) were added to refs/heads/master by this push:
     new ea86649  LOG4J2-2875 - Rollover was failing to create directories when using a DirectFileRolloverStrategy
ea86649 is described below

commit ea86649f8e073d1977a064c2d8e4a6a93df30ac4
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Thu Jul 30 10:41:30 2020 -0700

    LOG4J2-2875 - Rollover was failing to create directories when using a DirectFileRolloverStrategy
---
 .../logging/log4j/core/appender/FileManager.java   |  4 ++
 .../core/appender/rolling/RollingFileManager.java  |  7 +++
 .../appender/rolling/RollingNewDirectoryTest.java  | 62 ++++++++++++++++++++++
 .../test/resources/log4j-rolling-new-directory.xml | 39 ++++++++++++++
 src/changes/changes.xml                            |  3 ++
 5 files changed, 115 insertions(+)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
index 306b9a2..7359e5b 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
@@ -139,6 +139,7 @@ public class FileManager extends OutputStreamManager {
         final String filename = getFileName();
         LOGGER.debug("Now writing to {} at {}", filename, new Date());
         final File file = new File(filename);
+        createParentDir(file);
         final FileOutputStream fos = new FileOutputStream(file, isAppend);
         if (file.exists() && file.length() == 0) {
             try {
@@ -153,6 +154,9 @@ public class FileManager extends OutputStreamManager {
         return fos;
     }
 
+    protected void createParentDir(File file) {
+    }
+
     protected void defineAttributeView(final Path path) {
         if (attributeViewEnabled) {
             try {
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 e95dc85..4476d9b 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
@@ -175,6 +175,13 @@ public class RollingFileManager extends FileManager {
         return fileName;
     }
 
+    @Override
+    protected void createParentDir(File file) {
+        if (directWrite) {
+            file.getParentFile().mkdirs();
+        }
+    }
+
     public boolean isDirectWrite() {
         return directWrite;
     }
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingNewDirectoryTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingNewDirectoryTest.java
new file mode 100644
index 0000000..c1b9471
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingNewDirectoryTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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 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 java.io.File;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests
+ */
+public class RollingNewDirectoryTest {
+    private static final String CONFIG = "log4j-rolling-new-directory.xml";
+
+    private static final String DIR = "target/rolling-new-directory";
+
+    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(RollingNewDirectoryTest.class.getName());
+    }
+
+
+    @Test
+    public void streamClosedError() throws Exception {
+        for (int i = 0; i < 10; ++i) {
+            logger.info("AAA");
+            Thread.sleep(300);
+        }
+        final File dir = new File(DIR);
+        assertNotNull("No directory created", dir);
+        assertTrue("Child irectories not created", dir.exists() && dir.listFiles().length > 2);
+    }
+}
diff --git a/log4j-core/src/test/resources/log4j-rolling-new-directory.xml b/log4j-core/src/test/resources/log4j-rolling-new-directory.xml
new file mode 100644
index 0000000..8ac77e9
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-rolling-new-directory.xml
@@ -0,0 +1,39 @@
+<?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="WARN" name="XMLConfigTest">
+  <Properties>
+    <Property name="logDir">target/rolling-new-directory</Property>
+  </Properties>
+
+  <Appenders>
+    <RollingFile name="RollingFile" filePattern="${logDir}/%d{yyyy_MM_dd-mm-ss}/messages-$${date:yyyy_MM_dd_HH_mm_ss_SSS}.log">
+      <PatternLayout>
+        <Pattern>%d{MM-dd-yy-HH-mm-ss} %p %C{1.} [%t] %m%n</Pattern>
+      </PatternLayout>
+      <TimeBasedTriggeringPolicy />
+    </RollingFile>
+  </Appenders>
+
+  <Loggers>
+    <Root level="info">
+      <AppenderRef ref="RollingFile"/>
+    </Root>
+  </Loggers>
+
+</Configuration>
\ No newline at end of file
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ce80579..e2d6918 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -181,6 +181,9 @@
       </action>
     </release>
     <release version="2.14.0" date="2020-MM-DD" description="GA Release 2.14.0">
+      <action issue="LOG4J2-2875" dev="rgoers" type="fix">
+        Rollover was failing to create directories when using a DirectFileeRolloverStrategy.
+      </action>
       <action issue="LOG4J2-2859" dev="rgoers" type="fix" due-to="Yanming Zhou">
         Fixed typos where mergeFactory should be mergeStrategy.
       </action>