Skip to content

Commit b71c33e

Browse files
Benjamin PeterBenjamin Peter
authored andcommitted
Implement LocationAware requiresLocation method in FileLocationPatternConverter (#2781)
1 parent a884e99 commit b71c33e

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.core.pattern;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
import java.util.List;
24+
import org.apache.logging.log4j.core.impl.LocationAware;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.CsvSource;
27+
28+
public class LocationPatternConvertersRequireLocationText {
29+
30+
/**
31+
* Reproduces <a href="https://github.com/apache/logging-log4j2/issues/2781">github issue #2781</a>.
32+
*/
33+
@ParameterizedTest
34+
@CsvSource({"%L", "%l", "%F", "%C", "%M"})
35+
void testThatLocationDependentPatternConvertersIndicateLocationRequirement(final String converterKey)
36+
throws Exception {
37+
final PatternFormatter formatter = createSinglePatternFormatterFromConverterKey(converterKey);
38+
39+
final LocationAware locationAwareFormatter = assertInstanceOf(LocationAware.class, formatter.getConverter());
40+
assertTrue(locationAwareFormatter.requiresLocation());
41+
}
42+
43+
private static PatternFormatter createSinglePatternFormatterFromConverterKey(final String converterKey) {
44+
final PatternParser parser = new PatternParser("Converter");
45+
final List<PatternFormatter> formatters = parser.parse(converterKey);
46+
assertThat(formatters).hasSize(1);
47+
final PatternFormatter formatter = formatters.get(0);
48+
return formatter;
49+
}
50+
}

log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/FileLocationPatternConverter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818

1919
import org.apache.logging.log4j.core.LogEvent;
2020
import org.apache.logging.log4j.core.config.plugins.Plugin;
21+
import org.apache.logging.log4j.core.impl.LocationAware;
2122

2223
/**
2324
* Returns the event's line location information in a StringBuilder.
2425
*/
2526
@Plugin(name = "FileLocationPatternConverter", category = PatternConverter.CATEGORY)
2627
@ConverterKeys({"F", "file"})
27-
public final class FileLocationPatternConverter extends LogEventPatternConverter {
28+
public final class FileLocationPatternConverter extends LogEventPatternConverter implements LocationAware {
2829
/**
2930
* Singleton.
3031
*/
@@ -58,4 +59,9 @@ public void format(final LogEvent event, final StringBuilder output) {
5859
output.append(element.getFileName());
5960
}
6061
}
62+
63+
@Override
64+
public boolean requiresLocation() {
65+
return true;
66+
}
6167
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="fixed">
6+
<issue id="2781" link="https://github.com/apache/logging-log4j2/issues/2781"/>
7+
<description format="asciidoc">Fix handling of location requirement for pattern key `%F` / `%file`.</description>
8+
</entry>

0 commit comments

Comments
 (0)