You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2018/12/23 06:59:30 UTC

[logging-log4j2] branch release-2.x updated: LOG4J2-2485 - SizeBasedTriggeringPolicy was not honored when using the DirectWriteRolloverStrategy if the machine restarts

This is an automated email from the ASF dual-hosted git repository.

rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new f659f4a  LOG4J2-2485 - SizeBasedTriggeringPolicy was not honored when using the DirectWriteRolloverStrategy if the machine restarts
f659f4a is described below

commit f659f4ae13da2cc9feab7d4c7e9ab33be2ac55c9
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sat Dec 22 22:58:43 2018 -0700

    LOG4J2-2485 - SizeBasedTriggeringPolicy was not honored when using the DirectWriteRolloverStrategy if the machine restarts
---
 .../core/appender/rolling/RollingFileManager.java  |  7 +++
 .../RollingAppenderDirectWriteStartupSizeTest.java | 71 ++++++++++++++++++++++
 .../log4j-rolling-direct-startup-size.xml          | 35 +++++++++++
 src/changes/changes.xml                            |  3 +
 4 files changed, 116 insertions(+)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
index 4060a90..49ea71e 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
@@ -156,6 +156,13 @@ public class RollingFileManager extends FileManager {
             if (triggeringPolicy instanceof LifeCycle) {
                 ((LifeCycle) triggeringPolicy).start();
             }
+            if (rolloverStrategy instanceof DirectFileRolloverStrategy) {
+                // LOG4J2-2485: Initialize size from the most recently written file.
+                File file = new File(getFileName());
+                if (file.exists()) {
+                    size = file.length();
+                }
+            }
         }
     }
 
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteStartupSizeTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteStartupSizeTest.java
new file mode 100644
index 0000000..0e997ef
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteStartupSizeTest.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.appender.rolling;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import org.apache.logging.log4j.core.appender.RollingFileAppender;
+import org.apache.logging.log4j.junit.CleanFolders;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+
+/**
+ * Test LOG4J2-2485.
+ */
+public class RollingAppenderDirectWriteStartupSizeTest {
+
+  private static final String CONFIG = "log4j-rolling-direct-startup-size.xml";
+
+  private static final String DIR = "target/rolling-direct-startup-size";
+
+  private static final String FILE = "size-test.log";
+
+  private static final String MESSAGE = "test message";
+
+  @Rule
+  public LoggerContextRule loggerContextRule = LoggerContextRule
+      .createShutdownTimeoutLoggerContextRule(CONFIG);
+
+  @Rule
+  public CleanFolders cleanFolders = new CleanFolders(false, true, 10, DIR);
+
+  @BeforeClass
+  public static void beforeClass() throws Exception {
+    Path log = Paths.get(DIR, FILE);
+    if (Files.exists(log)) {
+        Files.delete(log);
+    }
+
+    Files.createDirectories(log.getParent());
+    Files.createFile(log);
+    Files.write(log, MESSAGE.getBytes());
+  }
+
+  @Test
+  public void testRollingFileAppenderWithReconfigure() throws Exception {
+    final RollingFileAppender rfAppender = loggerContextRule.getRequiredAppender("RollingFile",
+        RollingFileAppender.class);
+    final RollingFileManager manager = rfAppender.getManager();
+
+    Assert.assertNotNull(manager);
+    Assert.assertEquals("Existing file size not preserved on startup", MESSAGE.getBytes().length, manager.size);
+  }
+}
diff --git a/log4j-core/src/test/resources/log4j-rolling-direct-startup-size.xml b/log4j-core/src/test/resources/log4j-rolling-direct-startup-size.xml
new file mode 100644
index 0000000..4571c96
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-rolling-direct-startup-size.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one or more
+  ~ contributor license agreements. See the NOTICE file distributed with
+  ~ this work for additional information regarding copyright ownership.
+  ~ The ASF licenses this file to You under the Apache license, Version 2.0
+  ~ (the "License"); you may not use this file except in compliance with
+  ~ the License. You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the license for the specific language governing permissions and
+  ~ limitations under the license.
+  -->
+<Configuration status="INFO" name="RollingAppenderDirectWriteStartupSizeTest">
+  <Appenders>
+    <RollingFile name="RollingFile" filePattern="target/rolling-direct-startup-size/size-test.log">
+      <PatternLayout>
+        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
+      </PatternLayout>
+      <Policies>
+        <SizeBasedTriggeringPolicy size="250 MB"/>
+      </Policies>
+      <DirectWriteRolloverStrategy maxFiles="10"/>
+    </RollingFile>
+  </Appenders>
+  <Loggers>
+    <Root level="debug">
+      <AppenderRef ref="RollingFile"/>
+    </Root>
+  </Loggers>
+</Configuration>
\ No newline at end of file
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c11a311..0b2d603 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -31,6 +31,9 @@
          - "remove" - Removed
     -->
     <release version="2.11.2" date="2018-MM-DD" description="GA Release 2.11.2">
+      <action issue="LOG4J2-2485" dev="rgoers" type="fix" due-to="Giovanni Matteo Fumarola">
+        SizeBasedTriggeringPolicy was not honored when using the DirectWriteRolloverStrategy if the machine restarts.
+      </action>
       <action issue="LOG4J2-1906" dev="rgoers" type="fix">
         Direct write was creating files with the wrong date/time.
       </action>