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 2020/02/04 16:16:32 UTC
[logging-log4j2] branch release-2.x updated: LOG4J2-2759 -
RollingFileAppender was not rolling on startup if createOnDemand was set to
true.
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 d3c5919 LOG4J2-2759 - RollingFileAppender was not rolling on startup if createOnDemand was set to true.
d3c5919 is described below
commit d3c5919bd6eef869e4ada2c49cbc6976d268bc5d
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Tue Feb 4 09:16:10 2020 -0700
LOG4J2-2759 - RollingFileAppender was not rolling on startup if createOnDemand was set to true.
---
.../core/appender/rolling/RollingFileManager.java | 6 +-
.../rolling/RollingAppenderRestartTest.java | 76 ++++++++++++++++++++++
.../src/test/resources/log4j-rolling-restart.xml | 39 +++++++++++
src/changes/changes.xml | 3 +
4 files changed, 121 insertions(+), 3 deletions(-)
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 89a4c4c..5c7001d 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
@@ -337,7 +337,7 @@ public class RollingFileManager extends FileManager {
}
public synchronized void rollover() {
- if (!hasOutputStream()) {
+ if (!hasOutputStream() && !isCreateOnDemand()) {
return;
}
if (rollover(rolloverStrategy)) {
@@ -670,8 +670,8 @@ public class RollingFileManager extends FileManager {
final ByteBuffer buffer = ByteBuffer.wrap(new byte[actualSize]);
final OutputStream os = data.createOnDemand || data.fileName == null ? null :
new FileOutputStream(data.fileName, data.append);
- final long initialTime = data.createOnDemand || file == null ?
- 0 : initialFileTime(file); // LOG4J2-531 create file first so time has valid value
+ // LOG4J2-531 create file first so time has valid value.
+ final long initialTime = file == null || !file.exists() ? 0 : initialFileTime(file);
final RollingFileManager rm = new RollingFileManager(data.getLoggerContext(), data.fileName, data.pattern, os,
data.append, data.createOnDemand, size, initialTime, data.policy, data.strategy, data.advertiseURI,
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderRestartTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderRestartTest.java
new file mode 100644
index 0000000..ec8a32c
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderRestartTest.java
@@ -0,0 +1,76 @@
+/*
+ * 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 org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.hamcrest.Matcher;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.Month;
+import java.time.temporal.ChronoUnit;
+
+import static org.apache.logging.log4j.hamcrest.Descriptors.that;
+import static org.apache.logging.log4j.hamcrest.FileMatchers.hasName;
+import static org.hamcrest.Matchers.endsWith;
+import static org.hamcrest.Matchers.hasItemInArray;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ *
+ */
+public class RollingAppenderRestartTest {
+
+ private static final String CONFIG = "log4j-rolling-restart.xml";
+ private static final String DIR = "target/rolling-restart";
+
+ private final LoggerContextRule loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG);
+
+ @Rule
+ public RuleChain chain = loggerContextRule.withCleanFoldersRule(false, true, 5, DIR);
+
+ @BeforeClass
+ public static void setup() throws Exception {
+ File file = new File("target/rolling-restart/test.log");
+ Files.createDirectories(file.toPath().getParent());
+ Files.write(file.toPath(), "Hello, world".getBytes(), StandardOpenOption.CREATE);
+ long old = Instant.now().minus(2, ChronoUnit.DAYS).toEpochMilli();
+ assertTrue("Unable to set last modified time", file.setLastModified(old));
+ }
+
+ @Test
+ public void testAppender() throws Exception {
+ final Logger logger = loggerContextRule.getLogger();
+ logger.info("This is test message number 1");
+
+ final File dir = new File(DIR);
+
+ final Matcher<File[]> hasGzippedFile = hasItemInArray(that(hasName(that(endsWith(".gz")))));
+ final File[] files = dir.listFiles();
+ assertTrue("No gzipped files found", hasGzippedFile.matches(files));
+ }
+}
diff --git a/log4j-core/src/test/resources/log4j-rolling-restart.xml b/log4j-core/src/test/resources/log4j-rolling-restart.xml
new file mode 100644
index 0000000..eb65582
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-rolling-restart.xml
@@ -0,0 +1,39 @@
+<?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="ERROR" name="XMLConfigTest">
+ <Properties>
+ <Property name="filename">target/rolling-restart/test.log</Property>
+ </Properties>
+
+ <Appenders>
+ <RollingFile name="RollingFile" fileName="${filename}" filePattern="target/rolling-restart/test1-%d{MM-dd-yy}.log.gz" createOnDemand="true">
+ <PatternLayout>
+ <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
+ </PatternLayout>
+ <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
+ </RollingFile>
+ </Appenders>
+
+ <Loggers>
+ <Root level="info">
+ <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 bb65878..2eafcb8 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,6 +30,9 @@
- "remove" - Removed
-->
<release version="2.13.1" date="2019-MM-DD" description="GA Release 2.13.1">
+ <action issue="LOG4J2-2759" dev="rgoers" type="fix">
+ RollingFileAppender was not rolling on startup if createOnDemand was set to true.
+ </action>
<action issue="LOG4J2-2767" dev="rgoers" type="fix">
Warn if pattern is missing on Routes element. Use default route.
</action>