You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2024/08/06 11:18:04 UTC

(logging-log4j2) branch main updated: Add missing `FileLocationPatternConverter#requiresLocation()` (#2781, #2782)

This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/main by this push:
     new 24618b8c2f Add missing `FileLocationPatternConverter#requiresLocation()` (#2781, #2782)
24618b8c2f is described below

commit 24618b8c2f593f9356f8d1376e25b4ce08a0ea2f
Author: Benjamin Peter <ma...@benjaminpeter.name>
AuthorDate: Tue Aug 6 13:17:58 2024 +0200

    Add missing `FileLocationPatternConverter#requiresLocation()` (#2781, #2782)
    
    Co-authored-by: Benjamin Peter <B....@gsi.de>
    Co-authored-by: Volkan Yazıcı <vo...@yazi.ci>
---
 ...cationPatternConvertersRequireLocationTest.java | 52 ++++++++++++++++++++++
 .../core/pattern/FileLocationPatternConverter.java |  5 +++
 2 files changed, 57 insertions(+)

diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/LocationPatternConvertersRequireLocationTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/LocationPatternConvertersRequireLocationTest.java
new file mode 100644
index 0000000000..a8e86d40fc
--- /dev/null
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/LocationPatternConvertersRequireLocationTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.pattern;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.List;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+class LocationPatternConvertersRequireLocationTest {
+
+    private static final String KEY = "Converter";
+
+    private LoggerContext ctx = LoggerContext.getContext();
+
+    /**
+     * Reproduces <a href="https://github.com/apache/logging-log4j2/issues/2781">github issue #2781</a>.
+     */
+    @ParameterizedTest
+    @CsvSource({"%L", "%l", "%F", "%C", "%M"})
+    void testThatLocationDependentPatternConvertersIndicateLocationRequirement(final String converterKey)
+            throws Exception {
+        final PatternFormatter formatter = createSinglePatternFormatterFromConverterKey(converterKey);
+
+        assertTrue(formatter.requiresLocation());
+    }
+
+    private PatternFormatter createSinglePatternFormatterFromConverterKey(final String converterKey) {
+        final PatternParser parser = new PatternParser(ctx.getConfiguration(), KEY, null);
+        final List<PatternFormatter> formatters = parser.parse(converterKey);
+        assertEquals(1, formatters.size());
+        final PatternFormatter formatter = formatters.get(0);
+        return formatter;
+    }
+}
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/FileLocationPatternConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/FileLocationPatternConverter.java
index c23d48609a..f741de555e 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/FileLocationPatternConverter.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/FileLocationPatternConverter.java
@@ -60,4 +60,9 @@ public final class FileLocationPatternConverter extends LogEventPatternConverter
             output.append(element.getFileName());
         }
     }
+
+    @Override
+    public boolean requiresLocation() {
+        return true;
+    }
 }