Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.Set;
import java.util.concurrent.locks.LockSupport;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.util.Closer;
import org.apache.logging.log4j.core.util.FileUtils;
import org.apache.logging.log4j.core.util.NullOutputStream;
Expand Down Expand Up @@ -333,4 +334,40 @@ void testRolloverRetainsFileAttributes() throws Exception {
.permissions();
assertEquals(filePermissions, actualFilePermissions);
}

@Test
void testWriteHeaderWhenFileDoesNotExistBefore() throws IOException {
final File file = File.createTempFile("log4j2", "test");
file.delete(); // Ensure file doesn't exist
file.deleteOnExit();

final String header = "HEADER";
final PatternLayout layout = PatternLayout.newBuilder()
.setHeader(header)
.build();

final boolean isAppend = true;
final RollingRandomAccessFileManager manager = RollingRandomAccessFileManager.getRollingRandomAccessFileManager(
file.getAbsolutePath(),
Strings.EMPTY,
isAppend,
true,
RollingRandomAccessFileManager.DEFAULT_BUFFER_SIZE,
new SizeBasedTriggeringPolicy(Long.MAX_VALUE),
null,
null,
layout,
null,
null,
null,
null);
assertNotNull(manager);
manager.close();

// Verify header was written
assertTrue(file.exists(), "File should exist");
final byte[] fileContent = Files.readAllBytes(file.toPath());
final String content = new String(fileContent);
assertTrue(content.startsWith(header), "File should start with header: " + content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ public static RollingRandomAccessFileManager getRollingRandomAccessFileManager(
long size = 0;
long time = System.currentTimeMillis();
RandomAccessFile raf = null;
boolean fileExistedBefore = false;
if (fileName != null) {
file = new File(name);
fileExistedBefore = new File(name).exists();

if (!append) {
file.delete();
Expand Down Expand Up @@ -207,7 +209,7 @@ public static RollingRandomAccessFileManager getRollingRandomAccessFileManager(
return null;
}
}
final boolean writeHeader = !append || file == null || !file.exists();
final boolean writeHeader = !append || file == null || !fileExistedBefore;

final RollingRandomAccessFileManager rrm = new RollingRandomAccessFileManager(
data.getLoggerContext(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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">
<description format="asciidoc">
Fix writeHeader computation in RollingRandomAccessFileManager. The writeHeader boolean was computed after creating the RandomAccessFile, which creates the file if it doesn't exist. This made file.exists() always return true, causing incorrect header write decisions when append=true and the file didn't exist before.
</description>
</entry>