logging-log4j2
logging-log4j2 copied to clipboard
RandomAccessFileAppender does not write header to file
Description
RandomAccessFileAppender does not write the header configured in the layout to the output file.
Configuration
Version: 2.22.0
Operating system: Windows 10
JDK: Temurin 11.0.16.1
Reproduction
Using the following log4j2.xml:
<Configuration>
<Properties>
<!-- the pattern we use for our log files -->
<Property name="DefaultPattern">%d{DEFAULT} [%-5p] %c{1} - %m%n</Property>
<!-- a header written of the beginning of every log file -->
<Property name="DefaultHeader">*** HEADER ***${sys:line.separator}</Property>
</Properties>
<Appenders>
<File name="file" fileName="file.log" append="false">
<PatternLayout header="${DefaultHeader}" pattern="${DefaultPattern}" />
</File>
<RandomAccessFile name="randomaccessfile" fileName="randomaccessfile.log" append="false">
<PatternLayout header="${DefaultHeader}" pattern="${DefaultPattern}" />
</RandomAccessFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="file" />
<AppenderRef ref="randomaccessfile" />
</Root>
</Loggers>
</Configuration>
Combined with a simple program that just logs something, ie:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Test {
private static final Logger LOG = LogManager.getLogger();
public static void main(String[] args) {
LOG.info("Test");
}
}
This creates two files. The file.log is as expected:
*** HEADER ***
2023-11-30 16:54:04,898 [INFO ] Test - Test
But randomaccessfile.log does not contain the header line:
2023-11-30 16:54:04,898 [INFO ] Test - Test
MemoryMappedFileAppender also has the same issue.
Another issue from Jira for additional context: https://issues.apache.org/jira/browse/LOG4J2-3134 (can likely be rolled into this issue as we're migrating away from Jira over time).