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/10/19 06:19:24 UTC

[logging-log4j2] branch release-2.x updated: LOG4J2-2911 - Log4j2EventListener in spring.cloud.config.client listens for wrong event.

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 22be765  LOG4J2-2911 - Log4j2EventListener in spring.cloud.config.client listens for wrong event.
22be765 is described below

commit 22be765937e1a85431ce31ba48a5aa9cf25775b1
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sun Oct 18 23:17:13 2020 -0700

    LOG4J2-2911 - Log4j2EventListener in spring.cloud.config.client listens for wrong event.
---
 log4j-perf/pom.xml                                 |   2 +-
 .../cloud/config/client/Log4j2EventListener.java   |  18 ++--
 .../src/main/resources/META-INF/spring.factories   |   2 +-
 .../config/client/Log4j2EventListenerTest.java     | 114 +++++++++++++++++++++
 .../cloud/config/client/SpringConfiguration.java   |  26 +++++
 .../src/test/resources/log4j2-console.xml          |  31 ++++++
 log4j-spring-cloud-config/pom.xml                  |   2 +-
 pom.xml                                            |   7 +-
 src/changes/changes.xml                            |   3 +
 9 files changed, 189 insertions(+), 16 deletions(-)

diff --git a/log4j-perf/pom.xml b/log4j-perf/pom.xml
index c36524d..073d579 100644
--- a/log4j-perf/pom.xml
+++ b/log4j-perf/pom.xml
@@ -176,7 +176,7 @@
         <configuration>
           <toolchains>
             <jdk>
-              <version>[9, )</version>
+              <version>[11, )</version>
             </jdk>
           </toolchains>
         </configuration>
diff --git a/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/main/java/org/apache/logging/log4j/spring/cloud/config/client/Log4j2EventListener.java b/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/main/java/org/apache/logging/log4j/spring/cloud/config/client/Log4j2EventListener.java
index 96812d2..38b3843 100644
--- a/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/main/java/org/apache/logging/log4j/spring/cloud/config/client/Log4j2EventListener.java
+++ b/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/main/java/org/apache/logging/log4j/spring/cloud/config/client/Log4j2EventListener.java
@@ -23,27 +23,21 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.cloud.bus.ConditionalOnBusEnabled;
 import org.springframework.cloud.bus.SpringCloudBusClient;
 import org.springframework.cloud.bus.event.RemoteApplicationEvent;
+import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
 import org.springframework.cloud.stream.annotation.EnableBinding;
 import org.springframework.cloud.stream.annotation.StreamListener;
+import org.springframework.context.ApplicationListener;
 import org.springframework.context.event.EventListener;
 import org.springframework.stereotype.Component;
 
 @Component
-@ConditionalOnBusEnabled
-@EnableBinding(SpringCloudBusClient.class)
 @ConditionalOnProperty(value = "spring.cloud.config.watch.enabled")
-public class Log4j2EventListener {
+public class Log4j2EventListener implements ApplicationListener<EnvironmentChangeEvent> {
     private static Logger LOGGER = LogManager.getLogger(Log4j2EventListener.class);
 
-    @EventListener(classes = RemoteApplicationEvent.class)
-    public void acceptLocal(RemoteApplicationEvent event) {
-        LOGGER.debug("Refresh application event triggered");
-        WatchEventManager.publishEvent();
-    }
-
-    @StreamListener(SpringCloudBusClient.INPUT)
-    public void acceptRemote(RemoteApplicationEvent event) {
-        LOGGER.debug("Refresh application event triggered");
+    @Override
+    public void onApplicationEvent(EnvironmentChangeEvent environmentChangeEvent) {
+        LOGGER.debug("Application change event triggered");
         WatchEventManager.publishEvent();
     }
 }
diff --git a/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/main/resources/META-INF/spring.factories b/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/main/resources/META-INF/spring.factories
index 74309e8..22f28b8 100644
--- a/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/main/resources/META-INF/spring.factories
+++ b/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/main/resources/META-INF/spring.factories
@@ -14,4 +14,4 @@
 # See the license for the specific language governing permissions and
 # limitations under the license.
 #
-org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.apache.logging.log4j.spring.cloud.config.client.Log4j2EventListener
\ No newline at end of file
+org.springframework.context.ApplicationListener=org.apache.logging.log4j.spring.cloud.config.client.Log4j2EventListener
\ No newline at end of file
diff --git a/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/test/java/org/apache/logging/log4j/spring/cloud/config/client/Log4j2EventListenerTest.java b/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/test/java/org/apache/logging/log4j/spring/cloud/config/client/Log4j2EventListenerTest.java
new file mode 100644
index 0000000..98624c2
--- /dev/null
+++ b/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/test/java/org/apache/logging/log4j/spring/cloud/config/client/Log4j2EventListenerTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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.spring.cloud.config.client;
+
+import java.io.File;
+import java.util.HashSet;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.logging.log4j.core.config.ConfigurationListener;
+import org.apache.logging.log4j.core.config.Reconfigurable;
+import org.apache.logging.log4j.core.util.Source;
+import org.apache.logging.log4j.core.util.Watcher;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
+import org.springframework.context.ApplicationEventPublisher;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Class Description goes here.
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = {SpringConfiguration.class})
+public class Log4j2EventListenerTest {
+
+    private static final String CONFIG = "log4j-console.xml";
+    private static final String DIR = "target/logs";
+
+    public static LoggerContextRule loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG);
+
+    @Rule
+    public RuleChain chain = loggerContextRule.withCleanFilesRule(DIR);
+
+    @Autowired
+    private ApplicationEventPublisher publisher;
+
+    @Test
+    public void test() throws Exception {
+        AtomicInteger count = new AtomicInteger(0);
+       Source source = new Source(new File("test.java"));
+        loggerContextRule.getLoggerContext().getConfiguration().getWatchManager()
+                .watch(source, new TestWatcher(count));
+        publisher.publishEvent(new EnvironmentChangeEvent(new HashSet<>()));
+        assertTrue(count.get() > 0);
+    }
+
+    private static class TestWatcher implements Watcher {
+
+        private final AtomicInteger count;
+
+        public TestWatcher(AtomicInteger count) {
+            this.count = count;
+        }
+
+        @Override
+        public List<ConfigurationListener> getListeners() {
+            return null;
+        }
+
+        @Override
+        public void modified() {
+
+        }
+
+        @Override
+        public boolean isModified() {
+            count.incrementAndGet();
+            return false;
+        }
+
+        @Override
+        public long getLastModified() {
+            return 0;
+        }
+
+        @Override
+        public void watching(Source source) {
+
+        }
+
+        @Override
+        public Source getSource() {
+            return null;
+        }
+
+        @Override
+        public Watcher newWatcher(Reconfigurable reconfigurable, List<ConfigurationListener> listeners, long lastModifiedMillis) {
+            return this;
+        }
+    }
+}
diff --git a/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/test/java/org/apache/logging/log4j/spring/cloud/config/client/SpringConfiguration.java b/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/test/java/org/apache/logging/log4j/spring/cloud/config/client/SpringConfiguration.java
new file mode 100644
index 0000000..30437c9
--- /dev/null
+++ b/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/test/java/org/apache/logging/log4j/spring/cloud/config/client/SpringConfiguration.java
@@ -0,0 +1,26 @@
+/*
+ * 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.spring.cloud.config.client;
+
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * Needed to make Spring happy.
+ */
+@Configuration
+public class SpringConfiguration {
+}
diff --git a/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/test/resources/log4j2-console.xml b/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/test/resources/log4j2-console.xml
new file mode 100644
index 0000000..a528d25
--- /dev/null
+++ b/log4j-spring-cloud-config/log4j-spring-cloud-config-client/src/test/resources/log4j2-console.xml
@@ -0,0 +1,31 @@
+<?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="OFF">
+  <Appenders>
+    <Console name="Console" target="SYSTEM_OUT">
+      <PatternLayout pattern="%d [%t] %-5level: %msg%n%throwable" />
+    </Console>
+  </Appenders>
+  <Loggers>
+    <Logger name="org.foo" level="DEBUG" />
+    <Root level="TRACE">
+      <AppenderRef ref="Console" />
+    </Root>
+  </Loggers>
+</Configuration>
diff --git a/log4j-spring-cloud-config/pom.xml b/log4j-spring-cloud-config/pom.xml
index 3f76819..1d86e10 100644
--- a/log4j-spring-cloud-config/pom.xml
+++ b/log4j-spring-cloud-config/pom.xml
@@ -32,7 +32,7 @@
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <revapi.skip>true</revapi.skip>
     <spring-cloud-version>Hoxton.SR7</spring-cloud-version>
-    <spring-boot.version>2.3.3.RELEASE</spring-boot.version>
+    <spring-boot.version>2.3.4.RELEASE</spring-boot.version>
     <springVersion>5.2.8.RELEASE</springVersion>
     <log4jParentDir>${basedir}/..</log4jParentDir>
   </properties>
diff --git a/pom.xml b/pom.xml
index ac562e7..6dec19b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -189,7 +189,7 @@
     <logbackVersion>1.2.3</logbackVersion>
     <jackson1Version>1.9.13</jackson1Version>
     <jackson2Version>2.11.2</jackson2Version>
-    <spring-boot.version>2.3.3.RELEASE</spring-boot.version>
+    <spring-boot.version>2.3.4.RELEASE</spring-boot.version>
     <springVersion>5.2.8.RELEASE</springVersion>
     <kubernetes-client.version>4.6.1</kubernetes-client.version>
     <flumeVersion>1.9.0</flumeVersion>
@@ -1212,6 +1212,11 @@
             <artifactId>asciidoctor-maven-plugin</artifactId>
             <version>${asciidoc.plugin.version}</version>
           </dependency>
+          <dependency>
+            <groupId>org.asciidoctor</groupId>
+            <artifactId>asciidoctor-maven-plugin</artifactId>
+            <version>${asciidoc.plugin.version}</version>
+          </dependency>
         </dependencies>
         <configuration>
           <!-- only build English site even on other language OS -->
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6e35f55..f0f309b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,6 +30,9 @@
          - "remove" - Removed
     -->
     <release version="2.14.0" date="2020-MM-DD" description="GA Release 2.14.0">
+      <action issue="LOG4J2-2911" dev="rgoers" type="fix">
+        Log4j2EventListener in spring.cloud.config.client listens for wrong event.
+      </action>
       <action issue="LOG4J2-2889" dev="mattsicker" type="update" due-to="Geng Yuanzhe">
         Add date pattern support for HTML layout.
       </action>