ramanathan1504 commented on code in PR #4473:
URL: https://github.com/apache/logging-log4j2/pull/4473#discussion_r4535299841


##########
log4j-core/src/main/java/org/apache/logging/log4j/core/appender/MemoryMappedFileManager.java:
##########
@@ -327,13 +442,13 @@ protected void flushBuffer(final ByteBuffer buffer) {
 
     @Override
     public ByteBuffer getByteBuffer() {
-        return mappedBuffer;
+        return byteBuffer;

Review Comment:
   With `return mappedBuffer;` here all tests still pass: the encoder hits the 
NPE, logs a status error and falls back to `writeBytes()`. Can the tests check 
that no status error is logged? The `mappedBuffer != null` check in `flush()` 
and the `byteBuffer` loop in `write()` also pass every test when changed.



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RandomAccessFileManager.java:
##########
@@ -136,13 +181,37 @@ public void setEndOfBatch(@SuppressWarnings("unused") 
final boolean endOfBatch)
     @Override
     protected void writeToDestination(final byte[] bytes, final int offset, 
final int length) {
         try {
+            if (randomAccessFile == null) {
+                openFile();
+            }
             randomAccessFile.write(bytes, offset, length);
         } catch (final IOException ex) {
             final String msg = "Error writing to RandomAccessFile " + 
getName();
             throw new AppenderLoggingException(msg, ex);
         }
     }
 
+    @SuppressFBWarnings(
+            value = "PATH_TRAVERSAL_IN",
+            justification = "The name of the accessed file is based on a 
configuration value.")
+    private void openFile() throws IOException {
+        final File file = new File(getFileName());
+        FileUtils.makeParentDirs(file);
+        final RandomAccessFile raf = new RandomAccessFile(file, "rw");
+        try {
+            if (truncateOnOpen) {
+                raf.setLength(0);
+            } else {
+                raf.seek(raf.length());
+            }
+        } catch (final IOException e) {
+            Closer.closeSilently(raf);
+            throw e;
+        }
+        truncateOnOpen = false;

Review Comment:
   `openFile()` only runs while `randomAccessFile` is `null`, so this line is 
never read again.
   
   ```suggestion
   ```



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java:
##########
@@ -266,7 +280,9 @@ public static RollingRandomAccessFileManager 
getRollingRandomAccessFileManager(
                                     fileGroup,
                                     writeHeader);
                             rrm.setMaxRandomDelay(data.getMaxRandomDelay());
-                            if (rrm.isAttributeViewEnabled()) {
+                            // A `null` file is not open yet: 
`writeToDestination()` opens it on the first write.
+                            rrm.truncateOnOpen = raf == null && file != null 
&& !append;
+                            if (raf != null && rrm.isAttributeViewEnabled()) {

Review Comment:
   Without `raf != null` all tests still pass. Can a test with 
`filePermissions` and `createOnDemand` cover it?



##########
src/changelog/.2.x.x/3083_add_createOnDemand_to_file_appenders.xml:
##########
@@ -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">
+  <issue id="3083" 
link="https://github.com/apache/logging-log4j2/issues/3083"/>
+  <description format="asciidoc">
+    Add the `createOnDemand` attribute to the `RandomAccessFile`, 
`RollingRandomAccessFile` and `MemoryMappedFile` appenders, which defers the 
creation of the file and its parent directories until the first log event.

Review Comment:
   The entry needs the PR link, and the description can be one short line.
   
   ```suggestion
     <issue id="3083" 
link="https://github.com/apache/logging-log4j2/issues/3083"/>
     <issue id="4473" 
link="https://github.com/apache/logging-log4j2/pull/4473"/>
     <description format="asciidoc">
       Add `createOnDemand` to the remaining three file appenders.
   ```



-- 
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]

Reply via email to