Skip to content

Commit 3bc608d

Browse files
Propagate synchronous action failures in RollingFileManager and FileRenameAction (#1445, #1549)
1 parent 9886625 commit 3bc608d

File tree

5 files changed

+88
-6
lines changed

5 files changed

+88
-6
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121

2222
import org.apache.logging.log4j.core.LoggerContext;
2323
import org.apache.logging.log4j.core.appender.RollingFileAppender;
24+
import org.apache.logging.log4j.core.appender.rolling.action.AbstractAction;
2425
import org.apache.logging.log4j.core.config.Configuration;
26+
import org.apache.logging.log4j.core.config.NullConfiguration;
27+
import org.apache.logging.log4j.core.layout.PatternLayout;
2528
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
2629
import org.apache.logging.log4j.core.util.IOUtils;
2730
import org.junit.Assert;
@@ -84,4 +87,52 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
8487
}
8588
}
8689
}
90+
91+
/**
92+
* Test that a synchronous action failure does not cause a rollover. Addresses Issue #1445.
93+
*/
94+
@Test
95+
public void testSynchronousActionFailure() throws IOException {
96+
class FailingSynchronousAction extends AbstractAction {
97+
@Override
98+
public boolean execute() {
99+
return false;
100+
}
101+
}
102+
class FailingSynchronousStrategy implements RolloverStrategy {
103+
@Override
104+
public RolloverDescription rollover(final RollingFileManager manager) throws SecurityException {
105+
return new RolloverDescriptionImpl(manager.getFileName(), false, new FailingSynchronousAction(), null);
106+
}
107+
}
108+
109+
final Configuration configuration = new NullConfiguration();
110+
111+
// Create the manager.
112+
final File file = File.createTempFile("testSynchronousActionFailure", "log");
113+
final RollingFileManager manager = RollingFileManager.getFileManager(
114+
file.getAbsolutePath(),
115+
"testSynchronousActionFailure.log.%d{yyyy-MM-dd}", true, false,
116+
OnStartupTriggeringPolicy.createPolicy(1), new FailingSynchronousStrategy(), null,
117+
PatternLayout.createDefaultLayout(), 0, true, false, null, null, null, configuration);
118+
Assert.assertNotNull(manager);
119+
manager.initialize();
120+
121+
// Get the initialTime of this original log file
122+
final long initialTime = manager.getFileTime();
123+
124+
// Log something to ensure that the existing file size is > 0
125+
final String testContent = "Test";
126+
manager.writeToDestination(testContent.getBytes(StandardCharsets.US_ASCII), 0, testContent.length());
127+
128+
// Trigger rollover that will fail
129+
manager.rollover();
130+
131+
// If the rollover fails, then the size should not be reset
132+
Assert.assertNotEquals(0, manager.getFileSize());
133+
134+
// The initialTime should not have changed
135+
Assert.assertEquals(initialTime, manager.getFileTime());
136+
137+
}
87138
}

log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FileRenameActionTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public void testRename1() throws Exception {
4646

4747
final File dest = new File(tempDir, "newFile.log");
4848
final FileRenameAction action = new FileRenameAction(file, dest, false);
49-
action.execute();
49+
boolean renameResult = action.execute();
50+
assertTrue(renameResult, "Rename action returned false");
5051
assertTrue(dest.exists(), "Renamed file does not exist");
5152
assertFalse(file.exists(), "Old file exists");
5253
}
@@ -60,7 +61,8 @@ public void testEmpty() throws Exception {
6061
assertTrue(file.exists(), "File to rename does not exist");
6162
final File dest = new File(tempDir, "newFile.log");
6263
final FileRenameAction action = new FileRenameAction(file, dest, false);
63-
action.execute();
64+
boolean renameResult = action.execute();
65+
assertTrue(renameResult, "Rename action returned false");
6466
assertFalse(dest.exists(), "Renamed file should not exist");
6567
assertFalse(file.exists(), "Old file still exists");
6668
}
@@ -74,7 +76,8 @@ public void testRenameEmpty() throws Exception {
7476
assertTrue(file.exists(), "File to rename does not exist");
7577
final File dest = new File(tempDir, "newFile.log");
7678
final FileRenameAction action = new FileRenameAction(file, dest, true);
77-
action.execute();
79+
boolean renameResult = action.execute();
80+
assertTrue(renameResult, "Rename action returned false");
7881
assertTrue(dest.exists(), "Renamed file should exist");
7982
assertFalse(file.exists(), "Old file still exists");
8083
}
@@ -91,7 +94,8 @@ public void testNoParent() throws Exception {
9194

9295
final File dest = new File(tempDir, "newFile.log");
9396
final FileRenameAction action = new FileRenameAction(file, dest, false);
94-
action.execute();
97+
boolean renameResult = action.execute();
98+
assertTrue(renameResult, "Rename action returned false");
9599
assertTrue(dest.exists(), "Renamed file does not exist");
96100
assertFalse(file.exists(), "Old file exists");
97101
}

log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ private boolean rollover(final RolloverStrategy strategy) {
520520
asyncExecutor.execute(new AsyncAction(descriptor.getAsynchronous(), this));
521521
releaseRequired = false;
522522
}
523-
return true;
523+
return success;
524524
}
525525
return false;
526526
} finally {

log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/FileRenameAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public static boolean execute(final File source, final File destination, final b
163163
}
164164
} else {
165165
try {
166-
source.delete();
166+
return source.delete();
167167
} catch (final Exception exDelete) {
168168
LOGGER.error("Unable to delete empty file {}: {} {}", source.getAbsolutePath(),
169169
exDelete.getClass().getName(), exDelete.getMessage());
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xmlns="http://logging.apache.org/log4j/changelog"
20+
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.1.xsd"
21+
type="fixed">
22+
<issue id="1445" link="https://github.com/apache/logging-log4j2/issues/1445"/>
23+
<author id="thisdudeiknew" name="Timothy Pfeifer"/>
24+
<description format="asciidoc">
25+
Fixed `RollingFileManager` to propagate failed synchronous actions correctly.
26+
</description>
27+
</entry>

0 commit comments

Comments
 (0)