Merge branch '2.3.x'

Closes gh-21881
pull/21912/head
Madhura Bhave 5 years ago
commit f96a2f2b83

@ -20,8 +20,11 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
@ -31,11 +34,11 @@ import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger; import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory; import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.LoggerConfig; import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.filter.AbstractFilter; import org.apache.logging.log4j.core.filter.AbstractFilter;
import org.apache.logging.log4j.core.util.NameUtil;
import org.apache.logging.log4j.message.Message; import org.apache.logging.log4j.message.Message;
import org.springframework.boot.logging.LogFile; import org.springframework.boot.logging.LogFile;
@ -221,30 +224,65 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
@Override @Override
public List<LoggerConfiguration> getLoggerConfigurations() { public List<LoggerConfiguration> getLoggerConfigurations() {
Map<String, LoggerConfig> allLoggers = getAllLoggers();
List<LoggerConfiguration> result = new ArrayList<>(); List<LoggerConfiguration> result = new ArrayList<>();
Configuration configuration = getLoggerContext().getConfiguration(); allLoggers.forEach((key, value) -> result.add(convertLoggerConfiguration(value, key)));
for (LoggerConfig loggerConfig : configuration.getLoggers().values()) {
result.add(convertLoggerConfiguration(loggerConfig));
}
result.sort(CONFIGURATION_COMPARATOR); result.sort(CONFIGURATION_COMPARATOR);
return result; return result;
} }
private Map<String, LoggerConfig> getAllLoggers() {
Collection<Logger> loggers = getLoggerContext().getLoggers();
Map<String, LoggerConfig> configuredLoggers = getLoggerContext().getConfiguration().getLoggers();
Map<String, LoggerConfig> result = new LinkedHashMap<>();
for (Logger logger : loggers) {
String name = logger.getName();
while (name != null) {
result.putIfAbsent(name, getLoggerContext().getConfiguration().getLoggerConfig(name));
name = getSubName(name);
}
}
configuredLoggers.keySet().forEach((name) -> {
String currentName = name;
while (currentName != null) {
result.putIfAbsent(currentName, getLoggerContext().getConfiguration().getLoggerConfig(currentName));
currentName = getSubName(currentName);
}
});
return result;
}
private String getSubName(String name) {
if (StringUtils.isEmpty(name)) {
return null;
}
int nested = name.lastIndexOf('$');
if (nested != -1) {
return name.substring(0, nested);
}
return NameUtil.getSubName(name);
}
@Override @Override
public LoggerConfiguration getLoggerConfiguration(String loggerName) { public LoggerConfiguration getLoggerConfiguration(String loggerName) {
return convertLoggerConfiguration(getLoggerConfig(loggerName)); LoggerConfig loggerConfig = getAllLoggers().get(loggerName);
if (loggerConfig == null) {
return null;
}
return convertLoggerConfiguration(loggerConfig, loggerName);
} }
private LoggerConfiguration convertLoggerConfiguration(LoggerConfig loggerConfig) { private LoggerConfiguration convertLoggerConfiguration(LoggerConfig loggerConfig, String name) {
if (loggerConfig == null) { if (loggerConfig == null) {
return null; return null;
} }
LogLevel level = LEVELS.convertNativeToSystem(loggerConfig.getLevel()); LogLevel level = LEVELS.convertNativeToSystem(loggerConfig.getLevel());
String name = loggerConfig.getName();
if (!StringUtils.hasLength(name) || LogManager.ROOT_LOGGER_NAME.equals(name)) { if (!StringUtils.hasLength(name) || LogManager.ROOT_LOGGER_NAME.equals(name)) {
name = ROOT_LOGGER_NAME; name = ROOT_LOGGER_NAME;
} }
return new LoggerConfiguration(name, level, level); boolean isLoggerConfigured = loggerConfig.getName().equals(name);
LogLevel configuredLevel = (isLoggerConfigured) ? level : null;
return new LoggerConfiguration(name, configuredLevel, level);
} }
@Override @Override

@ -109,6 +109,7 @@ class DefaultLogbackConfiguration {
config.logger("org.apache.tomcat.util.net.NioSelectorPool", Level.WARN); config.logger("org.apache.tomcat.util.net.NioSelectorPool", Level.WARN);
config.logger("org.eclipse.jetty.util.component.AbstractLifeCycle", Level.ERROR); config.logger("org.eclipse.jetty.util.component.AbstractLifeCycle", Level.ERROR);
config.logger("org.hibernate.validator.internal.util.Version", Level.WARN); config.logger("org.hibernate.validator.internal.util.Version", Level.WARN);
config.logger("org.springframework.boot.actuate.endpoint.jmx", Level.WARN);
} }
private Appender<ILoggingEvent> consoleAppender(LogbackConfigurator config) { private Appender<ILoggingEvent> consoleAppender(LogbackConfigurator config) {

@ -18,4 +18,5 @@ Default logback configuration provided for import
<logger name="org.apache.tomcat.util.net.NioSelectorPool" level="WARN"/> <logger name="org.apache.tomcat.util.net.NioSelectorPool" level="WARN"/>
<logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="ERROR"/> <logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="ERROR"/>
<logger name="org.hibernate.validator.internal.util.Version" level="WARN"/> <logger name="org.hibernate.validator.internal.util.Version" level="WARN"/>
<logger name="org.springframework.boot.actuate.endpoint.jmx" level="WARN"/>
</included> </included>

@ -22,10 +22,14 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.LoggerContext;
@ -171,6 +175,28 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
assertThat(configurations.get(0).getName()).isEqualTo(LoggingSystem.ROOT_LOGGER_NAME); assertThat(configurations.get(0).getName()).isEqualTo(LoggingSystem.ROOT_LOGGER_NAME);
} }
@Test
void getLoggingConfigurationsShouldReturnAllLoggers() {
LogManager.getLogger("org.springframework.boot.logging.log4j2.Log4J2LoggingSystemTests$Nested");
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null, null);
this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG);
List<LoggerConfiguration> configurations = this.loggingSystem.getLoggerConfigurations();
assertThat(configurations).isNotEmpty();
assertThat(configurations.get(0).getName()).isEqualTo(LoggingSystem.ROOT_LOGGER_NAME);
Map<String, LogLevel> loggers = new LinkedHashMap<>();
configurations.forEach((logger) -> loggers.put(logger.getName(), logger.getConfiguredLevel()));
assertIsPresent("org", loggers, null);
assertIsPresent("org.springframework.boot.logging.log4j2", loggers, null);
assertIsPresent("org.springframework.boot.logging.log4j2.Log4J2LoggingSystemTests", loggers, LogLevel.DEBUG);
assertIsPresent("org.springframework.boot.logging.log4j2.Log4J2LoggingSystemTests$Nested", loggers, null);
}
private void assertIsPresent(String loggerName, Map<String, LogLevel> loggers, LogLevel logLevel) {
assertThat(loggers.containsKey(loggerName)).isTrue();
assertThat(loggers.get(loggerName)).isEqualTo(logLevel);
}
@Test @Test
void getLoggingConfiguration() { void getLoggingConfiguration() {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
@ -181,6 +207,24 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
.isEqualTo(new LoggerConfiguration(getClass().getName(), LogLevel.DEBUG, LogLevel.DEBUG)); .isEqualTo(new LoggerConfiguration(getClass().getName(), LogLevel.DEBUG, LogLevel.DEBUG));
} }
@Test
void getLoggingConfigurationShouldReturnLoggerWithNullConfiguredLevel() {
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null, null);
this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG);
LoggerConfiguration configuration = this.loggingSystem.getLoggerConfiguration("org");
assertThat(configuration).isEqualTo(new LoggerConfiguration("org", null, LogLevel.INFO));
}
@Test
void getLoggingConfigurationForNonExistentLoggerShouldReturnNull() {
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null, null);
this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG);
LoggerConfiguration configuration = this.loggingSystem.getLoggerConfiguration("doesnotexist");
assertThat(configuration).isEqualTo(null);
}
@Test @Test
void setLevelOfUnconfiguredLoggerDoesNotAffectRootConfiguration(CapturedOutput output) { void setLevelOfUnconfiguredLoggerDoesNotAffectRootConfiguration(CapturedOutput output) {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
@ -325,4 +369,14 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
} }
/**
* Used for testing that loggers in nested classes are returned by
* {@link Log4J2LoggingSystem#getLoggerConfigurations()} .
*/
static class Nested {
private static final Log logger = LogFactory.getLog(Nested.class);
}
} }

Loading…
Cancel
Save