Skip to content

Commit 62a2e17

Browse files
committed
Fix RollingFileManager and FileRenameAction to correctly propagate synchronous action failures
1 parent ae3d6e7 commit 62a2e17

File tree

5 files changed

+95
-9
lines changed

5 files changed

+95
-9
lines changed

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

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,24 @@
1616
*/
1717
package org.apache.logging.log4j.core.appender.rolling;
1818

19-
import java.io.*;
20-
import java.nio.charset.StandardCharsets;
21-
2219
import org.apache.logging.log4j.core.LoggerContext;
2320
import org.apache.logging.log4j.core.appender.RollingFileAppender;
21+
import org.apache.logging.log4j.core.appender.rolling.action.AbstractAction;
2422
import org.apache.logging.log4j.core.config.Configuration;
23+
import org.apache.logging.log4j.core.config.NullConfiguration;
24+
import org.apache.logging.log4j.core.layout.PatternLayout;
2525
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
2626
import org.apache.logging.log4j.core.util.IOUtils;
2727
import org.junit.Assert;
2828
import org.junit.Test;
2929

30+
import java.io.File;
31+
import java.io.FileInputStream;
32+
import java.io.IOException;
33+
import java.io.InputStreamReader;
34+
import java.io.Reader;
35+
import java.nio.charset.StandardCharsets;
36+
3037
public class RollingFileManagerTest {
3138

3239
/**
@@ -84,4 +91,52 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
8491
}
8592
}
8693
}
94+
95+
/**
96+
* Test that a synchronous action failure does not cause a rollover. Addresses Issue #1445.
97+
*/
98+
@Test
99+
public void testSynchronousActionFailure() throws IOException {
100+
class FailingSynchronousAction extends AbstractAction {
101+
@Override
102+
public boolean execute() {
103+
return false;
104+
}
105+
}
106+
class FailingSynchronousStrategy implements RolloverStrategy {
107+
@Override
108+
public RolloverDescription rollover(final RollingFileManager manager) throws SecurityException {
109+
return new RolloverDescriptionImpl(manager.getFileName(), false, new FailingSynchronousAction(), null);
110+
}
111+
}
112+
113+
final Configuration configuration = new NullConfiguration();
114+
115+
// Create the manager.
116+
final File file = File.createTempFile("testSynchronousActionFailure", "log");
117+
final RollingFileManager manager = RollingFileManager.getFileManager(
118+
file.getAbsolutePath(),
119+
"testSynchronousActionFailure.log.%d{yyyy-MM-dd}", true, false,
120+
OnStartupTriggeringPolicy.createPolicy(1), new FailingSynchronousStrategy(), null,
121+
PatternLayout.createDefaultLayout(), 0, true, false, null, null, null, configuration);
122+
Assert.assertNotNull(manager);
123+
manager.initialize();
124+
125+
// Get the initialTime of this original log file
126+
final long initialTime = manager.getFileTime();
127+
128+
// Log something to ensure that the existing file size is > 0
129+
final String testContent = "Test";
130+
manager.writeToDestination(testContent.getBytes(StandardCharsets.US_ASCII), 0, testContent.length());
131+
132+
// Trigger rollover that will fail
133+
manager.rollover();
134+
135+
// If the rollover fails, then the size should not be reset
136+
Assert.assertNotEquals(0, manager.getFileSize());
137+
138+
// The initialTime should not have changed
139+
Assert.assertEquals(initialTime, manager.getFileTime());
140+
141+
}
87142
}

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)