Commit 6391973e authored by Phillip Webb's avatar Phillip Webb

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
parent 8fd99bde
......@@ -56,6 +56,9 @@ public abstract class AbstractLoggingSystem extends LoggingSystem {
// Self initialization has occurred but the file has changed, reload
loadConfiguration(selfInitializationConfig, logFile);
}
else {
reinitialize();
}
}
}
......@@ -93,6 +96,15 @@ public abstract class AbstractLoggingSystem extends LoggingSystem {
*/
protected abstract void loadConfiguration(String location, LogFile logFile);
/**
* Reinitialize the logging system if required. Called when
* {@link #getSelfInitializationConfig()} is used and the log file hasn't changed. May
* be used to reload configuration (for example to pickup additional System
* properties).
*/
protected void reinitialize() {
}
protected final ClassLoader getClassLoader() {
return this.classLoader;
}
......
......@@ -33,7 +33,6 @@ import org.springframework.context.event.SmartApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.util.ClassUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ResourceUtils;
......@@ -98,6 +97,8 @@ public class LoggingApplicationListener implements SmartApplicationListener {
private final Log logger = LogFactory.getLog(getClass());
private LoggingSystem loggingSystem;
private int order = Ordered.HIGHEST_PRECEDENCE + 11;
private boolean parseArgs = true;
......@@ -126,10 +127,16 @@ public class LoggingApplicationListener implements SmartApplicationListener {
}
private void onApplicationStartedEvent(ApplicationStartedEvent event) {
LoggingSystem.get(ClassUtils.getDefaultClassLoader()).beforeInitialize();
this.loggingSystem = LoggingSystem.get(event.getSpringApplication()
.getClassLoader());
this.loggingSystem.beforeInitialize();
}
private void onApplicationPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
if (this.loggingSystem == null) {
this.loggingSystem = LoggingSystem.get(event.getSpringApplication()
.getClassLoader());
}
initialize(event.getEnvironment(), event.getSpringApplication().getClassLoader());
}
......@@ -142,9 +149,8 @@ public class LoggingApplicationListener implements SmartApplicationListener {
System.setProperty(PID_KEY, new ApplicationPid().toString());
}
initializeEarlyLoggingLevel(environment);
LoggingSystem system = LoggingSystem.get(classLoader);
initializeSystem(environment, system);
initializeFinalLoggingLevels(environment, system);
initializeSystem(environment, this.loggingSystem);
initializeFinalLoggingLevels(environment, this.loggingSystem);
}
private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) {
......
......@@ -46,8 +46,8 @@ public abstract class LoggingSystem {
/**
* Reset the logging system to be limit output. This method may be called before
* {@link #initialize(String, LogFile)} to reduce logging noise until the
* systems has been fully Initialized.
* {@link #initialize(String, LogFile)} to reduce logging noise until the system has
* been fully Initialized.
*/
public abstract void beforeInitialize();
......
......@@ -92,6 +92,11 @@ public class Log4JLoggingSystem extends Slf4JLoggingSystem {
}
}
@Override
protected void reinitialize() {
loadConfiguration(getSelfInitializationConfig(), null);
}
@Override
public void setLogLevel(String loggerName, LogLevel level) {
Logger logger = (StringUtils.hasLength(loggerName) ? LogManager
......
......@@ -23,9 +23,16 @@ import java.util.Map;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.filter.AbstractFilter;
import org.apache.logging.log4j.message.Message;
import org.springframework.boot.logging.LogFile;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem;
......@@ -55,6 +62,33 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
LEVELS = Collections.unmodifiableMap(levels);
}
private static final Filter FILTER = new AbstractFilter() {
@Override
public Result filter(LogEvent event) {
return Result.DENY;
};
@Override
public Result filter(Logger logger, Level level, Marker marker, Message msg,
Throwable t) {
return Result.DENY;
};
@Override
public Result filter(Logger logger, Level level, Marker marker, Object msg,
Throwable t) {
return Result.DENY;
};
@Override
public Result filter(Logger logger, Level level, Marker marker, String msg,
Object... params) {
return Result.DENY;
};
};
public Log4J2LoggingSystem(ClassLoader classLoader) {
super(classLoader);
}
......@@ -67,7 +101,13 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
@Override
public void beforeInitialize() {
super.beforeInitialize();
setLogLevel("", LogLevel.FATAL);
getLoggerConfig(null).addFilter(FILTER);
}
@Override
public void initialize(String configLocation, LogFile logFile) {
getLoggerConfig(null).removeFilter(FILTER);
super.initialize(configLocation, logFile);
}
@Override
......@@ -87,7 +127,7 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
logFile.applyToSystemProperties();
}
try {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
LoggerContext ctx = getLoggerContext();
URL url = ResourceUtils.getURL(location);
ConfigurationSource source = new ConfigurationSource(url.openStream(), url);
ctx.start(ConfigurationFactory.getInstance().getConfiguration(source));
......@@ -98,12 +138,25 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
}
}
@Override
protected void reinitialize() {
getLoggerContext().reconfigure();
}
@Override
public void setLogLevel(String loggerName, LogLevel level) {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
ctx.getConfiguration().getLoggerConfig(loggerName == null ? "" : loggerName)
.setLevel(LEVELS.get(level));
ctx.updateLoggers();
getLoggerConfig(loggerName).setLevel(LEVELS.get(level));
getLoggerContext().updateLoggers();
}
private LoggerConfig getLoggerConfig(String loggerName) {
LoggerConfig loggerConfig = getLoggerContext().getConfiguration()
.getLoggerConfig(loggerName == null ? "" : loggerName);
return loggerConfig;
}
private LoggerContext getLoggerContext() {
return (LoggerContext) LogManager.getContext(false);
}
}
......@@ -124,6 +124,12 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
}
}
@Override
protected void reinitialize() {
getLoggerContext().reset();
loadConfiguration(getSelfInitializationConfig(), null);
}
private void configureJBossLoggingToUseSlf4j() {
System.setProperty("org.jboss.logging.provider", "slf4j");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment