[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RandomAccessFileAppender does not write header to file #2020

Open
Xfel opened this issue Nov 30, 2023 · 2 comments
Open

RandomAccessFileAppender does not write header to file #2020

Xfel opened this issue Nov 30, 2023 · 2 comments

Comments

@Xfel
Copy link
Xfel commented Nov 30, 2023

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
@Xfel
Copy link
Author
Xfel commented Nov 30, 2023

MemoryMappedFileAppender also has the same issue.

@jvz
Copy link
Member
jvz commented Dec 1, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants