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/03 01:07:06 UTC

[logging-log4j2] branch release-2.x updated: LOG4J2-2767 - Warn if pattern is missing on Routes element. Use default route

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 1cfe13a  LOG4J2-2767 - Warn if pattern is missing on Routes element. Use default route
1cfe13a is described below

commit 1cfe13abf9498c42c20cf26c2fca31d6c1fc8034
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sun Feb 2 17:02:04 2020 -0700

    LOG4J2-2767 - Warn if pattern is missing on Routes element. Use default route
---
 .../log4j/core/appender/routing/Routes.java        |  2 +-
 .../core/appender/routing/RoutingAppender.java     |  3 +-
 .../appender/routing/RoutingAppender2767Test.java  | 70 ++++++++++++++++++++++
 .../src/test/resources/log4j-routing-2767.xml      | 53 ++++++++++++++++
 src/changes/changes.xml                            |  7 ++-
 5 files changed, 131 insertions(+), 4 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/routing/Routes.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/routing/Routes.java
index f02b7cf..fe54ef2 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/routing/Routes.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/routing/Routes.java
@@ -66,7 +66,7 @@ public final class Routes {
                 LOGGER.error("No Routes configured.");
                 return null;
             }
-            if (patternScript != null && pattern != null) {
+            if ((patternScript != null && pattern != null) || (patternScript == null && pattern == null)) {
                 LOGGER.warn("In a Routes element, you must configure either a Script element or a pattern attribute.");
             }
             if (patternScript != null) {
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java
index 9a71f9d..b33723a 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java
@@ -224,7 +224,8 @@ public final class RoutingAppender extends AbstractAppender {
             event = rewritePolicy.rewrite(event);
         }
         final String pattern = routes.getPattern(event, scriptStaticVariables);
-        final String key = pattern != null ? configuration.getStrSubstitutor().replace(event, pattern) : defaultRoute.getKey();
+        final String key = pattern != null ? configuration.getStrSubstitutor().replace(event, pattern) :
+                defaultRoute.getKey() != null ? defaultRoute.getKey() : DEFAULT_KEY;
         final RouteAppenderControl control = getControl(key, event);
         if (control != null) {
             try {
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender2767Test.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender2767Test.java
new file mode 100644
index 0000000..1348351
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender2767Test.java
@@ -0,0 +1,70 @@
+/*
+ * 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.routing;
+
+import org.apache.logging.log4j.EventLogger;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.apache.logging.log4j.message.StructuredDataMessage;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class RoutingAppender2767Test {
+    private static final String CONFIG = "log4j-routing-2767.xml";
+    private static final String ACTIVITY_LOG_FILE = "target/routing1/routingtest-Service.log";
+
+    private final LoggerContextRule loggerContextRule = new LoggerContextRule(CONFIG);
+
+    @Rule
+    public RuleChain rules = loggerContextRule.withCleanFilesRule(ACTIVITY_LOG_FILE);
+
+    @Before
+    public void setUp() throws Exception {
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        this.loggerContextRule.getLoggerContext().stop();
+    }
+
+    @Test
+    public void routingTest() throws Exception {
+        StructuredDataMessage msg = new StructuredDataMessage("Test", "This is a test", "Service");
+        EventLogger.logEvent(msg);
+        File file = new File(ACTIVITY_LOG_FILE);
+        assertTrue("Activity file was not created", file.exists());
+        List<String> lines = Files.lines(file.toPath()).collect(Collectors.toList());
+        assertEquals("Incorrect number of lines", 1, lines.size());
+        assertTrue("Incorrect content", lines.get(0).contains("This is a test"));
+    }
+}
diff --git a/log4j-core/src/test/resources/log4j-routing-2767.xml b/log4j-core/src/test/resources/log4j-routing-2767.xml
new file mode 100644
index 0000000..34b9c30
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-routing-2767.xml
@@ -0,0 +1,53 @@
+<?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="WARN" name="RoutingTest">
+  <Properties>
+    <Property name="filename">target/routing1/routingtest-$${sd:type}.log</Property>
+  </Properties>
+
+  <Appenders>
+    <Console name="STDOUT">
+      <PatternLayout pattern="%m%n"/>
+    </Console>
+    <Routing name="Routing">
+      <Routes>
+        <Route>
+          <RollingFile name="Routing-${sd:type}" fileName="${filename}"
+                       filePattern="target/routing1/test1-${sd:type}.%i.log.gz">
+            <PatternLayout>
+              <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
+            </PatternLayout>
+            <SizeBasedTriggeringPolicy size="500" />
+          </RollingFile>
+        </Route>
+      </Routes>
+    </Routing>
+  </Appenders>
+
+  <Loggers>
+    <Logger name="EventLogger" level="info" additivity="false">
+      <AppenderRef ref="Routing"/>
+    </Logger>
+
+    <Root level="info">
+      <AppenderRef ref="Routing"/>
+    </Root>
+  </Loggers>
+
+</Configuration>
\ No newline at end of file
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 082fde4..53d98a6 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-2767" dev="rgoers" type="fix">
+        Warn if pattern is missing on Routes element. Use default route.
+      </action>
       <action issue="LOG4J2-2415" dev="ckozak" type="fix" due-to="Andrey Turbanov">
         Fix lock contention in the classloader using new versions of slf4j without EventData on slf4j logger creation.
       </action>
@@ -66,10 +69,10 @@
       <action issue="LOG4J2-2575" dev="rgoers" type="fix" due-to="Nathan Friess">
         CronExpression.getBeforeTime() would sometimes return incorrect result.
       </action>
-      <action issue="LOG4J2-2762" dev="ggregory" type="fix" due-to="Gary Gregory">
+      <action issue="LOG4J2-2762" dev="ggregory" type="fix">
         [JDBC] MS-SQL Server JDBC driver throws SQLServerException when inserting a null value for a VARBINARY column.
       </action>
-      <action issue="LOG4J2-2763" dev="ggregory" type="update" due-to="Gary Gregory">
+      <action issue="LOG4J2-2763" dev="ggregory" type="update">
         Update dependencies.
       </action>
     </release>