toyeshhm opened a new pull request, #4469: URL: https://github.com/apache/logging-log4j2/pull/4469
Fixes #2020. Also covers `MemoryMappedFileAppender`, reported in the same issue, and [LOG4J2-3134](https://issues.apache.org/jira/browse/LOG4J2-3134). `RandomAccessFileAppender` and `MemoryMappedFileAppender` never write the layout header. Both managers pass `NullOutputStream` as `os` to `OutputStreamManager`, and the `OutputStreamManager` constructor writes the header to `os`, so it is thrown away. The footer goes through `write()` and does reach the file, so a `JsonLayout` with `complete="true"` ends up with a closing `]` and no opening `[`. `RandomAccessFileManager` and `MemoryMappedFileManager` now pass `writeHeader = false` to the superclass and write the header themselves at the end of the constructor, once the file (or the mapped buffer) is set up. They use `write()`, the same path the footer takes. The `writeHeader` flag the factories already compute is unchanged, so appending to an existing file still adds no header. `RollingRandomAccessFileManager` already works around this in its own `writeHeader()`. ## Tests `RandomAccessFileManagerTest#testWriteHeader` and `MemoryMappedFileManagerTest#testWriteHeader` are parameterized over `append` and whether the file already exists, like `RollingRandomAccessFileManagerTest#testWriteHeaderWhetherAppendOrExists`. Each one writes a line and checks the whole file. Before the fix, the three cases that need a header fail with `expected: <HEADER\nDATA\n> but was: <DATA\n>`. Appending to an existing file passes before and after. ## Checklist * Base your changes on `2.x` branch if you are targeting Log4j 2; use `main` otherwise * `./mvnw verify` succeeds ([the build instructions](https://logging.apache.org/log4j/2.x/development.html#building)) * Non-trivial changes contain an entry file in the `src/changelog/.2.x.x` directory * Tests are provided -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
