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 2023/01/23 16:33:37 UTC
[logging-log4j2] branch master updated: LOG4J2-3643 - When using a Date Lookup in the file pattern the current time should be used.
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new 7dc8b1d67a LOG4J2-3643 - When using a Date Lookup in the file pattern the current time should be used.
7dc8b1d67a is described below
commit 7dc8b1d67a50f1ffcaf0c478498e046b43112c1e
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Mon Jan 23 09:33:26 2023 -0700
LOG4J2-3643 - When using a Date Lookup in the file pattern the current time should be used.
---
.../RollingAppenderCronAndSizeLookupTest.java | 91 ++++++++++++++++++++++
.../log4j-rolling-cron-and-size-lookup.xml | 56 +++++++++++++
.../core/appender/rolling/PatternProcessor.java | 4 +-
...ingFileAppender_DateLookup_use_current_time.xml | 22 ++++++
4 files changed, 171 insertions(+), 2 deletions(-)
diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderCronAndSizeLookupTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderCronAndSizeLookupTest.java
new file mode 100644
index 0000000000..bd803f32d9
--- /dev/null
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderCronAndSizeLookupTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.io.File;
+import java.util.Arrays;
+import java.util.Random;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+
+import static org.apache.logging.log4j.core.test.hamcrest.Descriptors.that;
+import static org.apache.logging.log4j.core.test.hamcrest.FileMatchers.hasName;
+import static org.hamcrest.Matchers.endsWith;
+import static org.hamcrest.Matchers.hasItemInArray;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * LOG4J2-1804.
+ */
+public class RollingAppenderCronAndSizeLookupTest {
+
+ private static final String CONFIG = "log4j-rolling-cron-and-size-lookup.xml";
+
+ private static final String DIR = "target/rolling-cron-size-lookup";
+
+ public static LoggerContextRule loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG);
+
+ @Rule
+ public RuleChain chain = loggerContextRule.withCleanFoldersRule(DIR);
+
+ private Logger logger;
+
+ @Before
+ public void setUp() throws Exception {
+ this.logger = loggerContextRule.getLogger(RollingAppenderCronAndSizeLookupTest.class.getName());
+ }
+
+ @Test
+ public void testAppender() throws Exception {
+ Random rand = new Random();
+ // Loop for 500 times with a 5ms wait guarantees at least 2 time based rollovers.
+ for (int j=0; j < 500; ++j) {
+ for (int i = 0; i < 10; ++i) {
+ logger.debug("This is test message number " + i);
+ }
+ Thread.sleep(5);
+ }
+ Thread.sleep(50);
+ final File dir = new File(DIR);
+ assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
+ final File[] files = dir.listFiles();
+ Arrays.sort(files);
+ assertNotNull(files);
+ assertThat(files, hasItemInArray(that(hasName(that(endsWith(".log"))))));
+ int found = 0;
+ int fileCounter = 0;
+ String previous = "";
+ for (final File file: files) {
+ final String actual = file.getName();
+ if (previous.length() == 0) {
+ previous = actual;
+ } else {
+ assertNotSame("File names snould not be equal", previous, actual);
+ }
+ }
+
+ }
+}
diff --git a/log4j-core-test/src/test/resources/log4j-rolling-cron-and-size-lookup.xml b/log4j-core-test/src/test/resources/log4j-rolling-cron-and-size-lookup.xml
new file mode 100644
index 0000000000..4c4079835e
--- /dev/null
+++ b/log4j-core-test/src/test/resources/log4j-rolling-cron-and-size-lookup.xml
@@ -0,0 +1,56 @@
+<?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="RollingCronTest" monitorInterval="1">
+ <Properties>
+ <Property name="filename">target/rolling1/rollingtest.log</Property>
+ <Property name="DIR">target/rolling-cron-size-lookup</Property>
+ </Properties>
+ <Filters>
+ <ThresholdFilter level="debug"/>
+ </Filters>
+
+ <Appenders>
+ <Console name="STDOUT">
+ <PatternLayout pattern="%m%n"/>
+ </Console>
+ <RollingFile name="RollingFile" fileName="${filename}" filePattern="${DIR}/rollingfile_$${date:yyyy-MM-dd-HH-mm-ss-SSS}.log">
+ <PatternLayout pattern="%d{yyyy-MM-dd-HH-mm-ss} [%level] [%c] %msg%n"/>
+ <Policies>
+ <CronTriggeringPolicy schedule="* * * * * ?"/>
+ <SizeBasedTriggeringPolicy size="5000" />
+ </Policies>
+ </RollingFile>
+ <List name="List">
+ <Filters>
+ <ThresholdFilter level="error"/>
+ </Filters>
+ </List>
+ </Appenders>
+
+ <Loggers>
+ <Logger name="org.apache.logging.log4j.core.appender.rolling" level="debug" additivity="false">
+ <AppenderRef ref="RollingFile"/>
+ </Logger>
+
+ <Root level="error">
+ <AppenderRef ref="STDOUT"/>
+ </Root>
+ </Loggers>
+
+</Configuration>
\ No newline at end of file
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
index ed73e0fe3c..3b2b680d9d 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
@@ -304,8 +304,8 @@ public class PatternProcessor {
final long time = useCurrentTime ? currentFileTime != 0 ? currentFileTime : System.currentTimeMillis() :
prevFileTime != 0 ? prevFileTime : System.currentTimeMillis();
formatFileName(buf, new Date(time), obj);
- final LogEvent event = new Log4jLogEvent.Builder().setTimeMillis(time).build();
- final String fileName = subst.replace(event, buf);
+ // LOG4J2-3643
+ final String fileName = subst.replace(null, buf);
buf.setLength(0);
buf.append(fileName);
}
diff --git a/src/changelog/.2.x.x/LOG4J2-3643_RollingFileAppender_DateLookup_use_current_time.xml b/src/changelog/.2.x.x/LOG4J2-3643_RollingFileAppender_DateLookup_use_current_time.xml
new file mode 100644
index 0000000000..d65fa4d658
--- /dev/null
+++ b/src/changelog/.2.x.x/LOG4J2-3643_RollingFileAppender_DateLookup_use_current_time.xml
@@ -0,0 +1,22 @@
+<?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.
+-->
+<entry type="fix">
+ <issue id="LOG4J2-3643" link="https://issues.apache.org/jira/browse/LOG4J2-3643"/>
+ <author id="rgoers"/>
+ <description format="asciidoc">When using a Date Lookup in the file pattern the current time should be used.</description>
+</entry>