Reset StatusLogger fallback listener stream on initialization
Update `Log4J2LoggingSystem` so that the `StatusLogger` fallback listener has its print stream reset on each initialization. This allows output capture to work with the status listener. Fixes gh-43578 Co-authored-by: Phillip Webb <phil.webb@broadcom.com>
This commit is contained in:
committed by
Phillip Webb
parent
ec75474abf
commit
b6b9237f2c
@@ -35,8 +35,8 @@ import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
@ClassPathExclusions("log4j-to-slf4j-*.jar")
|
||||
@ClassPathOverrides({ "org.apache.logging.log4j:log4j-core:2.19.0",
|
||||
"org.apache.logging.log4j:log4j-slf4j-impl:2.19.0" })
|
||||
@ClassPathOverrides({ "org.apache.logging.log4j:log4j-core:2.24.3",
|
||||
"org.apache.logging.log4j:log4j-slf4j-impl:2.24.3" })
|
||||
public @interface ConfigureClasspathToPreferLog4j2 {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -47,6 +47,7 @@ import org.apache.logging.log4j.core.net.ssl.SslConfigurationFactory;
|
||||
import org.apache.logging.log4j.core.util.AuthorizationProvider;
|
||||
import org.apache.logging.log4j.core.util.NameUtil;
|
||||
import org.apache.logging.log4j.jul.Log4jBridgeHandler;
|
||||
import org.apache.logging.log4j.status.StatusLogger;
|
||||
import org.apache.logging.log4j.util.PropertiesUtil;
|
||||
|
||||
import org.springframework.boot.context.properties.bind.BindResult;
|
||||
@@ -213,6 +214,7 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
|
||||
if (isAlreadyInitialized(loggerContext)) {
|
||||
return;
|
||||
}
|
||||
resetFallbackListenerStream(StatusLogger.getLogger());
|
||||
Environment environment = initializationContext.getEnvironment();
|
||||
if (environment != null) {
|
||||
getLoggerContext().putObject(ENVIRONMENT_KEY, environment);
|
||||
@@ -224,6 +226,21 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
|
||||
markAsInitialized(loggerContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the stream used by the fallback listener to the current system out. This
|
||||
* allows the fallback lister to work with any captured output streams in a similar
|
||||
* way to the {@code follow} attribute of the {@code literal Console} appender.
|
||||
* @param statusLogger the status logger to update
|
||||
*/
|
||||
private void resetFallbackListenerStream(StatusLogger statusLogger) {
|
||||
try {
|
||||
statusLogger.getFallbackListener().setStream(System.out);
|
||||
}
|
||||
catch (NoSuchMethodError ex) {
|
||||
// Ignore for older versions of Log4J
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) {
|
||||
String location = getPackagedConfigFile((logFile != null) ? "log4j2-file.xml" : "log4j2.xml");
|
||||
|
||||
Reference in New Issue
Block a user