Skip to content

Commit ff11e21

Browse files
committed
Polish "Improve Log4j core configuration file detection for Log4j 3"
See gh-46409
1 parent 16439ad commit ff11e21

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,51 +158,52 @@ public Log4J2LoggingSystem(ClassLoader classLoader) {
158158
@Override
159159
protected String[] getStandardConfigLocations() {
160160
List<String> locations = new ArrayList<>();
161-
// The `log4j2.configurationFile` and `log4j.configuration.location` properties
162-
// should be checked first, as they can be set to a custom location.
163-
for (String property : new String[] { "log4j2.configurationFile", "log4j.configuration.location" }) {
161+
addLocationsFromProperties(locations);
162+
addStandardLocations(locations);
163+
return StringUtils.toStringArray(locations);
164+
}
165+
166+
private void addLocationsFromProperties(List<String> locations) {
167+
for (String property : List.of("log4j2.configurationFile", "log4j.configuration.location")) {
164168
String propertyDefinedLocation = PropertiesUtil.getProperties().getStringProperty(property);
165169
if (propertyDefinedLocation != null) {
166170
locations.add(propertyDefinedLocation);
167171
}
168172
}
173+
}
169174

170-
// If no custom location is defined, we use the standard locations.
175+
private void addStandardLocations(List<String> locations) {
171176
LoggerContext loggerContext = getLoggerContext();
172177
String contextName = loggerContext.getName();
173178
List<String> extensions = getStandardConfigExtensions();
174179
extensions.forEach((e) -> locations.add("log4j2-test" + contextName + e));
175180
extensions.forEach((e) -> locations.add("log4j2-test" + e));
176181
extensions.forEach((e) -> locations.add("log4j2" + contextName + e));
177182
extensions.forEach((e) -> locations.add("log4j2" + e));
178-
179-
return StringUtils.toStringArray(locations);
180183
}
181184

182185
private List<String> getStandardConfigExtensions() {
183186
List<String> extensions = new ArrayList<>();
184187
// These classes need to be visible by the classloader that loads Log4j Core.
185188
ClassLoader classLoader = LoggerContext.class.getClassLoader();
186-
// The order of the extensions corresponds to the order
187-
// in which Log4j Core 2 and 3 will try to load them,
188-
// in decreasing value of `@Order`.
189+
// The order of the extensions corresponds to the order in which Log4j Core 2 and
190+
// 3 will try to load them, in decreasing value of @Order.
189191
if (isClassAvailable(classLoader, PROPS_CONFIGURATION_FACTORY_V2)
190192
|| isClassAvailable(classLoader, PROPS_CONFIGURATION_FACTORY_V3)) {
191193
extensions.add(".properties");
192194
}
193-
if (areClassesAvailable(classLoader, YAML_CONFIGURATION_FACTORY_V2, YAML_TREE_PARSER_V2)
195+
if (areAllClassesAvailable(classLoader, YAML_CONFIGURATION_FACTORY_V2, YAML_TREE_PARSER_V2)
194196
|| isClassAvailable(classLoader, YAML_CONFIGURATION_FACTORY_V3)) {
195197
Collections.addAll(extensions, ".yaml", ".yml");
196198
}
197199
if (isClassAvailable(classLoader, JSON_TREE_PARSER_V2) || isClassAvailable(classLoader, JSON_TREE_PARSER_V3)) {
198200
Collections.addAll(extensions, ".json", ".jsn");
199201
}
200-
// We assume the `java.xml` module is always available.
201202
extensions.add(".xml");
202203
return extensions;
203204
}
204205

205-
private boolean areClassesAvailable(ClassLoader classLoader, String... classNames) {
206+
private boolean areAllClassesAvailable(ClassLoader classLoader, String... classNames) {
206207
for (String className : classNames) {
207208
if (!isClassAvailable(classLoader, className)) {
208209
return false;
@@ -211,6 +212,11 @@ private boolean areClassesAvailable(ClassLoader classLoader, String... className
211212
return true;
212213
}
213214

215+
@Deprecated(since = "4.0.0", forRemoval = true)
216+
protected boolean isClassAvailable(String className) {
217+
return ClassUtils.isPresent(className, getClassLoader());
218+
}
219+
214220
protected boolean isClassAvailable(ClassLoader classLoader, String className) {
215221
return ClassUtils.isPresent(className, classLoader);
216222
}

0 commit comments

Comments
 (0)