Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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,11 +41,14 @@
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;
import org.apache.logging.log4j.util.Strings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

/**
* Tests the RollingRandomAccessFileManager class.
Expand Down Expand Up @@ -333,4 +336,62 @@ void testRolloverRetainsFileAttributes() throws Exception {
.permissions();
assertEquals(filePermissions, actualFilePermissions);
}

@ParameterizedTest
@CsvSource({
"true,true",
"true,false",
"false,true",
"false,false",
})
void testWriteHeaderWhetherAppendOrExists(final boolean append, final boolean fileExists) throws Exception {
final File file = File.createTempFile("log4j2", "test");
if (!fileExists) {
file.delete(); // Ensure file doesn't exist
} else {
// If file exists, write some content to it so it's not empty
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write("EXISTING_CONTENT".getBytes());
}
}
file.deleteOnExit();

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

final RollingRandomAccessFileManager manager = RollingRandomAccessFileManager.getRollingRandomAccessFileManager(
file.getAbsolutePath(),
Strings.EMPTY,
append,
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 based on logic: writeHeader = !append || !fileExistedBefore
// Note: writeHeader() also checks file.length() == 0, so header is only written if file is empty
assertTrue(file.exists(), "File should exist");
final byte[] fileContent = Files.readAllBytes(file.toPath());
final String content = new String(fileContent);
final boolean expectedHeaderWritten = !append || !fileExists;
if (expectedHeaderWritten) {
assertTrue(content.startsWith(header), "File should start with header when append=" + append
+ ", fileExists=" + fileExists + ", content: " + content);
} else {
// When append=true and fileExists=true, file has existing content, so header should not be written
assertTrue(!content.startsWith(header),
"File should not start with header when append=" + append + ", fileExists=" + fileExists
+ ", content: " + 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 = file.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 header write in `RollingRandomAccessFileManager` that was being incorrectly skipped if `append=true` and the file didn't exist before.
</description>
</entry>

Loading