本文主要研究一下springboot的logging.group
org/springframework/boot/actuate/logging/LoggersEndpoint.java
@Endpoint(id = "loggers")
public class LoggersEndpoint {
private final LoggingSystem loggingSystem;
private final LoggerGroups loggerGroups;
/**
* Create a new {@link LoggersEndpoint} instance.
* @param loggingSystem the logging system to expose
* @param loggerGroups the logger group to expose
*/
public LoggersEndpoint(LoggingSystem loggingSystem, LoggerGroups loggerGroups) {
Assert.notNull(loggingSystem, "LoggingSystem must not be null");
Assert.notNull(loggerGroups, "LoggerGroups must not be null");
this.loggingSystem = loggingSystem;
this.loggerGroups = loggerGroups;
}
@ReadOperation
public Map loggers() {
Collection configurations = this.loggingSystem.getLoggerConfigurations();
if (configurations == null) {
return Collections.emptyMap();
}
Map result = new LinkedHashMap<>();
result.put("levels", getLevels());
result.put("loggers", getLoggers(configurations));
result.put("groups", getGroups());
return result;
}
private Map getGroups() {
Map groups = new LinkedHashMap<>();
this.loggerGroups.forEach((group) -> groups.put(group.getName(),
new GroupLoggerLevels(group.getConfiguredLevel(), group.getMembers())));
return groups;
}
@ReadOperation
public LoggerLevels loggerLevels(@Selector String name) {
Assert.notNull(name, "Name must not be null");
LoggerGroup group = this.loggerGroups.get(name);
if (group != null) {
return new GroupLoggerLevels(group.getConfiguredLevel(), group.getMembers());
}
LoggerConfiguration configuration = this.loggingSystem.getLoggerConfiguration(name);
return (configuration != null) ? new SingleLoggerLevels(configuration) : null;
}
@WriteOperation
public void configureLogLevel(@Selector String name, @Nullable LogLevel configuredLevel) {
Assert.notNull(name, "Name must not be empty");
LoggerGroup group = this.loggerGroups.get(name);
if (group != null && group.hasMembers()) {
group.configureLogLevel(configuredLevel, this.loggingSystem::setLogLevel);
return;
}
this.loggingSystem.setLogLevel(name, configuredLevel);
}
//......
}
LoggersEndpoint提供了loggers、loggerLevels、configureLogLevel方法
org/springframework/boot/logging/LoggerGroups.java
public final class LoggerGroups implements Iterable {
private final Map groups = new ConcurrentHashMap<>();
public LoggerGroups() {
}
public LoggerGroups(Map> namesAndMembers) {
putAll(namesAndMembers);
}
public void putAll(Map> namesAndMembers) {
namesAndMembers.forEach(this::put);
}
private void put(String name, List members) {
put(new LoggerGroup(name, members));
}
private void put(LoggerGroup loggerGroup) {
this.groups.put(loggerGroup.getName(), loggerGroup);
}
public LoggerGroup get(String name) {
return this.groups.get(name);
}
@Override
public Iterator iterator() {
return this.groups.values().iterator();
}
}
LoggerGroups实现了Iterable接口,其泛型为LoggerGroup
org/springframework/boot/logging/LoggerGroup.java
public final class LoggerGroup {
private final String name;
private final List members;
private LogLevel configuredLevel;
LoggerGroup(String name, List members) {
this.name = name;
this.members = Collections.unmodifiableList(new ArrayList<>(members));
}
public String getName() {
return this.name;
}
public List getMembers() {
return this.members;
}
public boolean hasMembers() {
return !this.members.isEmpty();
}
public LogLevel getConfiguredLevel() {
return this.configuredLevel;
}
public void configureLogLevel(LogLevel level, BiConsumer configurer) {
this.configuredLevel = level;
this.members.forEach((name) -> configurer.accept(name, level));
}
}
LoggerGroup定义了name、members、configuredLevel属性,其configureLogLevel会遍历members,通过configurer(
LoggingSystem接口定义了setLogLevel方法
)去变更level
org/springframework/boot/logging/logback/LogbackLoggingSystem.java
@Override
public void setLogLevel(String loggerName, LogLevel level) {
ch.qos.logback.classic.Logger logger = getLogger(loggerName);
if (logger != null) {
logger.setLevel(LEVELS.convertSystemToNative(level));
}
}
LogbackLoggingSystem的setLogLevel委托给了logger.setLevel
ch/qos/logback/classic/Logger.java
public synchronized void setLevel(Level newLevel) {
if (level == newLevel) {
// nothing to do;
return;
}
if (newLevel == null && isRootLogger()) {
throw new IllegalArgumentException("The level of the root logger cannot be set to null");
}
level = newLevel;
if (newLevel == null) {
effectiveLevelInt = parent.effectiveLevelInt;
newLevel = parent.getEffectiveLevel();
} else {
effectiveLevelInt = newLevel.levelInt;
}
if (childrenList != null) {
int len = childrenList.size();
for (int i = 0; i < len; i++) {
Logger child = (Logger) childrenList.get(i);
// tell child to handle parent levelInt change
child.handleParentLevelChange(effectiveLevelInt);
}
}
// inform listeners
loggerContext.fireOnLevelChange(this, newLevel);
}
setLevel先判断当前level是否需要变更,不需要直接返回,之后变更effectiveLevelInt为newLevel.levelInt,若该logger有childrenList,则触发child.handleParentLevelChange(effectiveLevelInt),最后执行loggerContext.fireOnLevelChange(this, newLevel)。
org/springframework/boot/context/logging/LoggingApplicationListener.java
public class LoggingApplicationListener implements GenericApplicationListener {
private static final ConfigurationPropertyName LOGGING_LEVEL = ConfigurationPropertyName.of("logging.level");
private static final ConfigurationPropertyName LOGGING_GROUP = ConfigurationPropertyName.of("logging.group");
private static final Bindable
LoggingApplicationListener继承了GenericApplicationListener,它定义了DEFAULT_GROUP_LOGGERS,默认定义了web、sql两个LoggerGroup
{
"levels": [
"OFF",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
],
"loggers": {
"ROOT": {
"configuredLevel": "INFO",
"effectiveLevel": "INFO"
},
"Validator": {
"configuredLevel": null,
"effectiveLevel": "INFO"
}
},
"groups": {
"web": {
"configuredLevel": null,
"members": [
"org.springframework.core.codec",
"org.springframework.http",
"org.springframework.web",
"org.springframework.boot.actuate.endpoint.web",
"org.springframework.boot.web.servlet.ServletContextInitializerBeans"
]
},
"sql": {
"configuredLevel": null,
"members": [
"org.springframework.jdbc.core",
"org.hibernate.SQL",
"org.jooq.tools.LoggerListener"
]
}
}
}
springboot的LoggersEndpoint提供了loggers、loggerLevels、configureLogLevel方法;LoggingApplicationListener继承了GenericApplicationListener,它定义了DEFAULT_GROUP_LOGGERS,默认定义了web、sql两个LoggerGroup;configureLogLevel方法可以传group名,也可以传具体的logger名,如果是group,则会一次变更其所有members的level。