Skip to content

Commit ec017d0

Browse files
committed
Make logging system factory ordering more robust
Closes gh-44689
1 parent a35c04a commit ec017d0

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/java/JavaLoggingSystem.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void cleanUp() {
176176
/**
177177
* {@link LoggingSystemFactory} that returns {@link JavaLoggingSystem} if possible.
178178
*/
179-
@Order(Ordered.LOWEST_PRECEDENCE)
179+
@Order(Ordered.LOWEST_PRECEDENCE - 1024)
180180
public static class Factory implements LoggingSystemFactory {
181181

182182
private static final boolean PRESENT = ClassUtils.isPresent("java.util.logging.LogManager",

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
import org.springframework.boot.logging.LoggingSystem;
6464
import org.springframework.boot.logging.LoggingSystemFactory;
6565
import org.springframework.core.Conventions;
66-
import org.springframework.core.Ordered;
6766
import org.springframework.core.annotation.Order;
6867
import org.springframework.core.env.Environment;
6968
import org.springframework.core.io.Resource;
@@ -494,7 +493,7 @@ public static Environment getEnvironment(LoggerContext loggerContext) {
494493
/**
495494
* {@link LoggingSystemFactory} that returns {@link Log4J2LoggingSystem} if possible.
496495
*/
497-
@Order(Ordered.LOWEST_PRECEDENCE)
496+
@Order(0)
498497
public static class Factory implements LoggingSystemFactory {
499498

500499
private static final boolean PRESENT = ClassUtils

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ void setStatusPrinterStream(PrintStream stream) {
483483
/**
484484
* {@link LoggingSystemFactory} that returns {@link LogbackLoggingSystem} if possible.
485485
*/
486-
@Order(Ordered.LOWEST_PRECEDENCE)
486+
@Order(Ordered.HIGHEST_PRECEDENCE + 1024)
487487
public static class Factory implements LoggingSystemFactory {
488488

489489
private static final boolean PRESENT = ClassUtils.isPresent("ch.qos.logback.classic.LoggerContext",

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,10 @@
2020
import org.junit.jupiter.api.Test;
2121

2222
import org.springframework.boot.logging.LoggingSystem.NoOpLoggingSystem;
23+
import org.springframework.boot.logging.java.JavaLoggingSystem;
24+
import org.springframework.boot.logging.log4j2.Log4J2LoggingSystem;
2325
import org.springframework.boot.logging.logback.LogbackLoggingSystem;
26+
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
2427

2528
import static org.assertj.core.api.Assertions.assertThat;
2629
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -42,6 +45,18 @@ void logbackIsTheDefaultLoggingSystem() {
4245
assertThat(LoggingSystem.get(getClass().getClassLoader())).isInstanceOf(LogbackLoggingSystem.class);
4346
}
4447

48+
@Test
49+
@ClassPathExclusions("logback-*.jar")
50+
void log4j2IsUsedInTheAbsenceOfLogback() {
51+
assertThat(LoggingSystem.get(getClass().getClassLoader())).isInstanceOf(Log4J2LoggingSystem.class);
52+
}
53+
54+
@Test
55+
@ClassPathExclusions({ "logback-*.jar", "log4j-*.jar" })
56+
void julIsUsedInTheAbsenceOfLogbackAndLog4j2() {
57+
assertThat(LoggingSystem.get(getClass().getClassLoader())).isInstanceOf(JavaLoggingSystem.class);
58+
}
59+
4560
@Test
4661
void loggingSystemCanBeDisabled() {
4762
System.setProperty(LoggingSystem.SYSTEM_PROPERTY, LoggingSystem.NONE);

0 commit comments

Comments
 (0)