Skip to content

Commit a4a8e99

Browse files
authored
Fix StatusLogger to correctly read log4j2.StatusLogger.properties (#2354)
1 parent 054a4f7 commit a4a8e99

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

log4j-api-test/src/test/java/org/apache/logging/log4j/status/StatusLoggerPropertiesUtilDoubleTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.logging.log4j.status;
1818

19+
import static java.util.Collections.singletonMap;
1920
import static org.apache.logging.log4j.status.StatusLogger.PropertiesUtilsDouble.readAllAvailableProperties;
2021
import static org.apache.logging.log4j.status.StatusLogger.PropertiesUtilsDouble.readProperty;
2122
import static org.assertj.core.api.Assertions.assertThat;
@@ -24,7 +25,9 @@
2425

2526
import java.util.Arrays;
2627
import java.util.Map;
28+
import java.util.Properties;
2729
import java.util.stream.Stream;
30+
import org.junit.jupiter.api.Test;
2831
import org.junit.jupiter.api.parallel.ResourceAccessMode;
2932
import org.junit.jupiter.api.parallel.ResourceLock;
3033
import org.junit.jupiter.api.parallel.Resources;
@@ -109,4 +112,11 @@ private static void verifyProperty(final TestCase testCase, final String expecte
109112
assertThat(actualValue).describedAs("" + testCase).isNull();
110113
}
111114
}
115+
116+
@Test
117+
void properties_file_in_class_path_should_be_read() {
118+
final String propertiesFileName = StatusLoggerPropertiesUtilDoubleTest.class.getSimpleName() + ".properties";
119+
final Properties actualProperties = StatusLogger.PropertiesUtilsDouble.readPropertiesFile(propertiesFileName);
120+
assertThat(actualProperties).containsExactlyEntriesOf(singletonMap("foo", "bar"));
121+
}
112122
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
18+
#
19+
# This file only exists to test if `StatusLogger.PropertiesUtilDouble` indeed loads the resource at the root directory.
20+
# Hence, don't place this file under a subdirectory!
21+
#
22+
foo=bar

log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ static String readProperty(final Map<String, Object> normalizedProperties, final
404404
static Map<String, Object> readAllAvailableProperties() {
405405
final Properties systemProperties = System.getProperties();
406406
final Properties environmentProperties = readEnvironmentProperties();
407-
final Properties fileProvidedProperties = readPropertiesFile();
407+
final Properties fileProvidedProperties = readPropertiesFile(PROPERTIES_FILE_NAME);
408408
return normalizeProperties(systemProperties, environmentProperties, fileProvidedProperties);
409409
}
410410

@@ -419,16 +419,20 @@ private static Properties readEnvironmentProperties() {
419419
// Consequently, they would delegate to `LoaderUtil`, etc.
420420
// All these mechanisms expect a working `StatusLogger`.
421421
// Hence, in order to be self-sufficient, we cannot rely on them.
422-
private static Properties readPropertiesFile() {
422+
static Properties readPropertiesFile(final String propertiesFileName) {
423423
final Properties properties = new Properties();
424-
final URL url = StatusLogger.class.getResource(PROPERTIES_FILE_NAME);
424+
// Unlike `ClassLoader#getResource()`, which takes absolute resource paths, `Class#getResource()` supports
425+
// relative resource paths. Without a `/` prefix, the resource must be placed into JAR resources as
426+
// `org/apache/logging/log4j/status/log4j2.StatusLogger.properties`. Hence, the `/` prefix.
427+
final String resourceName = '/' + propertiesFileName;
428+
final URL url = StatusLogger.class.getResource(resourceName);
425429
if (url == null) {
426430
return properties;
427431
}
428432
try (final InputStream stream = url.openStream()) {
429433
properties.load(stream);
430434
} catch (final IOException error) {
431-
final String message = String.format("failed reading properties from `%s`", PROPERTIES_FILE_NAME);
435+
final String message = String.format("failed reading properties from `%s`", propertiesFileName);
432436
final RuntimeException extendedError = new RuntimeException(message, error);
433437
// There is no logging system at this stage.
434438
// There is nothing we can do but simply dumping the failure.
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="http://logging.apache.org/log4j/changelog"
4+
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.3.xsd"
5+
type="fixed">
6+
<issue id="2354" link="https://github.com/apache/logging-log4j2/pull/2354"/>
7+
<description format="asciidoc">Fix `StatusLogger` to correctly read `log4j2.StatusLogger.properties` resource</description>
8+
</entry>

0 commit comments

Comments
 (0)