Skip to content

Commit 6391973

Browse files
committed
Always reinitialize logging system
Update LoggingSystems so that they can reinitialize themselves before logging begins. This allows reset of the root logger (which may have been set to OFF in beforeInitialize) and also re-evaluation of systems properties such as PID which may not have been set when logging configuration was first loaded. This commit may possibly reintroduce gh-1091, but it seems like reloading is our only option. Fixes gh-2125
1 parent 8fd99bd commit 6391973

File tree

6 files changed

+95
-13
lines changed

6 files changed

+95
-13
lines changed

spring-boot/src/main/java/org/springframework/boot/logging/AbstractLoggingSystem.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ else if (logFile != null) {
5656
// Self initialization has occurred but the file has changed, reload
5757
loadConfiguration(selfInitializationConfig, logFile);
5858
}
59+
else {
60+
reinitialize();
61+
}
5962
}
6063
}
6164

@@ -93,6 +96,15 @@ protected String getSelfInitializationConfig() {
9396
*/
9497
protected abstract void loadConfiguration(String location, LogFile logFile);
9598

99+
/**
100+
* Reinitialize the logging system if required. Called when
101+
* {@link #getSelfInitializationConfig()} is used and the log file hasn't changed. May
102+
* be used to reload configuration (for example to pickup additional System
103+
* properties).
104+
*/
105+
protected void reinitialize() {
106+
}
107+
96108
protected final ClassLoader getClassLoader() {
97109
return this.classLoader;
98110
}

spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.core.Ordered;
3434
import org.springframework.core.env.ConfigurableEnvironment;
3535
import org.springframework.core.env.Environment;
36-
import org.springframework.util.ClassUtils;
3736
import org.springframework.util.LinkedMultiValueMap;
3837
import org.springframework.util.MultiValueMap;
3938
import org.springframework.util.ResourceUtils;
@@ -98,6 +97,8 @@ public class LoggingApplicationListener implements SmartApplicationListener {
9897

9998
private final Log logger = LogFactory.getLog(getClass());
10099

100+
private LoggingSystem loggingSystem;
101+
101102
private int order = Ordered.HIGHEST_PRECEDENCE + 11;
102103

103104
private boolean parseArgs = true;
@@ -126,10 +127,16 @@ else if (event instanceof ApplicationEnvironmentPreparedEvent) {
126127
}
127128

128129
private void onApplicationStartedEvent(ApplicationStartedEvent event) {
129-
LoggingSystem.get(ClassUtils.getDefaultClassLoader()).beforeInitialize();
130+
this.loggingSystem = LoggingSystem.get(event.getSpringApplication()
131+
.getClassLoader());
132+
this.loggingSystem.beforeInitialize();
130133
}
131134

132135
private void onApplicationPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
136+
if (this.loggingSystem == null) {
137+
this.loggingSystem = LoggingSystem.get(event.getSpringApplication()
138+
.getClassLoader());
139+
}
133140
initialize(event.getEnvironment(), event.getSpringApplication().getClassLoader());
134141
}
135142

@@ -142,9 +149,8 @@ protected void initialize(ConfigurableEnvironment environment, ClassLoader class
142149
System.setProperty(PID_KEY, new ApplicationPid().toString());
143150
}
144151
initializeEarlyLoggingLevel(environment);
145-
LoggingSystem system = LoggingSystem.get(classLoader);
146-
initializeSystem(environment, system);
147-
initializeFinalLoggingLevels(environment, system);
152+
initializeSystem(environment, this.loggingSystem);
153+
initializeFinalLoggingLevels(environment, this.loggingSystem);
148154
}
149155

150156
private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) {

spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public abstract class LoggingSystem {
4646

4747
/**
4848
* Reset the logging system to be limit output. This method may be called before
49-
* {@link #initialize(String, LogFile)} to reduce logging noise until the
50-
* systems has been fully Initialized.
49+
* {@link #initialize(String, LogFile)} to reduce logging noise until the system has
50+
* been fully Initialized.
5151
*/
5252
public abstract void beforeInitialize();
5353

spring-boot/src/main/java/org/springframework/boot/logging/log4j/Log4JLoggingSystem.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ protected void loadConfiguration(String location, LogFile logFile) {
9292
}
9393
}
9494

95+
@Override
96+
protected void reinitialize() {
97+
loadConfiguration(getSelfInitializationConfig(), null);
98+
}
99+
95100
@Override
96101
public void setLogLevel(String loggerName, LogLevel level) {
97102
Logger logger = (StringUtils.hasLength(loggerName) ? LogManager

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

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@
2323

2424
import org.apache.logging.log4j.Level;
2525
import org.apache.logging.log4j.LogManager;
26+
import org.apache.logging.log4j.Marker;
27+
import org.apache.logging.log4j.core.Filter;
28+
import org.apache.logging.log4j.core.LogEvent;
29+
import org.apache.logging.log4j.core.Logger;
2630
import org.apache.logging.log4j.core.LoggerContext;
2731
import org.apache.logging.log4j.core.config.ConfigurationFactory;
2832
import org.apache.logging.log4j.core.config.ConfigurationSource;
33+
import org.apache.logging.log4j.core.config.LoggerConfig;
34+
import org.apache.logging.log4j.core.filter.AbstractFilter;
35+
import org.apache.logging.log4j.message.Message;
2936
import org.springframework.boot.logging.LogFile;
3037
import org.springframework.boot.logging.LogLevel;
3138
import org.springframework.boot.logging.LoggingSystem;
@@ -55,6 +62,33 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
5562
LEVELS = Collections.unmodifiableMap(levels);
5663
}
5764

65+
private static final Filter FILTER = new AbstractFilter() {
66+
67+
@Override
68+
public Result filter(LogEvent event) {
69+
return Result.DENY;
70+
};
71+
72+
@Override
73+
public Result filter(Logger logger, Level level, Marker marker, Message msg,
74+
Throwable t) {
75+
return Result.DENY;
76+
};
77+
78+
@Override
79+
public Result filter(Logger logger, Level level, Marker marker, Object msg,
80+
Throwable t) {
81+
return Result.DENY;
82+
};
83+
84+
@Override
85+
public Result filter(Logger logger, Level level, Marker marker, String msg,
86+
Object... params) {
87+
return Result.DENY;
88+
};
89+
90+
};
91+
5892
public Log4J2LoggingSystem(ClassLoader classLoader) {
5993
super(classLoader);
6094
}
@@ -67,7 +101,13 @@ protected String[] getStandardConfigLocations() {
67101
@Override
68102
public void beforeInitialize() {
69103
super.beforeInitialize();
70-
setLogLevel("", LogLevel.FATAL);
104+
getLoggerConfig(null).addFilter(FILTER);
105+
}
106+
107+
@Override
108+
public void initialize(String configLocation, LogFile logFile) {
109+
getLoggerConfig(null).removeFilter(FILTER);
110+
super.initialize(configLocation, logFile);
71111
}
72112

73113
@Override
@@ -87,7 +127,7 @@ protected void loadConfiguration(String location, LogFile logFile) {
87127
logFile.applyToSystemProperties();
88128
}
89129
try {
90-
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
130+
LoggerContext ctx = getLoggerContext();
91131
URL url = ResourceUtils.getURL(location);
92132
ConfigurationSource source = new ConfigurationSource(url.openStream(), url);
93133
ctx.start(ConfigurationFactory.getInstance().getConfiguration(source));
@@ -98,12 +138,25 @@ protected void loadConfiguration(String location, LogFile logFile) {
98138
}
99139
}
100140

141+
@Override
142+
protected void reinitialize() {
143+
getLoggerContext().reconfigure();
144+
}
145+
101146
@Override
102147
public void setLogLevel(String loggerName, LogLevel level) {
103-
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
104-
ctx.getConfiguration().getLoggerConfig(loggerName == null ? "" : loggerName)
105-
.setLevel(LEVELS.get(level));
106-
ctx.updateLoggers();
148+
getLoggerConfig(loggerName).setLevel(LEVELS.get(level));
149+
getLoggerContext().updateLoggers();
150+
}
151+
152+
private LoggerConfig getLoggerConfig(String loggerName) {
153+
LoggerConfig loggerConfig = getLoggerContext().getConfiguration()
154+
.getLoggerConfig(loggerName == null ? "" : loggerName);
155+
return loggerConfig;
156+
}
157+
158+
private LoggerContext getLoggerContext() {
159+
return (LoggerContext) LogManager.getContext(false);
107160
}
108161

109162
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ protected void loadConfiguration(String location, LogFile logFile) {
124124
}
125125
}
126126

127+
@Override
128+
protected void reinitialize() {
129+
getLoggerContext().reset();
130+
loadConfiguration(getSelfInitializationConfig(), null);
131+
}
132+
127133
private void configureJBossLoggingToUseSlf4j() {
128134
System.setProperty("org.jboss.logging.provider", "slf4j");
129135
}

0 commit comments

Comments
 (0)