Index: log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteStartupSizeTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteStartupSizeTest.java (date 1541106037007) +++ log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteStartupSizeTest.java (date 1541106037007) @@ -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); + } +} Index: log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java (date 1533309554000) +++ log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java (date 1541105989689) @@ -151,6 +151,13 @@ 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(); + } + } } } Index: log4j-core/src/test/resources/log4j-rolling-direct-startup-size.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/test/resources/log4j-rolling-direct-startup-size.xml (date 1541103985936) +++ log4j-core/src/test/resources/log4j-rolling-direct-startup-size.xml (date 1541103985936) @@ -0,0 +1,35 @@ + + + + + + + %d %p %c{1.} [%t] %m%n + + + + + + + + + + + + + \ No newline at end of file