聊聊springboot的LoggersEndpoint

2023-11-03 15:56:50 浏览数 (3)

本文主要研究一下springboot的LoggersEndpoint

LoggersEndpoint

代码语言:javascript复制
/**
 * {@link Endpoint @Endpoint} to expose a collection of {@link LoggerConfiguration}s.
 *
 * @author Ben Hale
 * @author Phillip Webb
 * @author HaiTao Zhang
 * @since 2.0.0
 */
@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;
	}

	//......
}	

springboot的actuator定义了LoggersEndpoint,它构造器依赖loggingSystem及loggerGroups

loggers

代码语言:javascript复制
	@ReadOperation
	public Map<String, Object> loggers() {
		Collection<LoggerConfiguration> configurations = this.loggingSystem.getLoggerConfigurations();
		if (configurations == null) {
			return Collections.emptyMap();
		}
		Map<String, Object> result = new LinkedHashMap<>();
		result.put("levels", getLevels());
		result.put("loggers", getLoggers(configurations));
		result.put("groups", getGroups());
		return result;
	}

	private NavigableSet<LogLevel> getLevels() {
		Set<LogLevel> levels = this.loggingSystem.getSupportedLogLevels();
		return new TreeSet<>(levels).descendingSet();
	}

	private Map<String, LoggerLevels> getLoggers(Collection<LoggerConfiguration> configurations) {
		Map<String, LoggerLevels> loggers = new LinkedHashMap<>(configurations.size());
		for (LoggerConfiguration configuration : configurations) {
			loggers.put(configuration.getName(), new SingleLoggerLevels(configuration));
		}
		return loggers;
	}

	private Map<String, LoggerLevels> getGroups() {
		Map<String, LoggerLevels> groups = new LinkedHashMap<>();
		this.loggerGroups.forEach((group) -> groups.put(group.getName(),
				new GroupLoggerLevels(group.getConfiguredLevel(), group.getMembers())));
		return groups;
	}	

LoggersEndpoint定义了loggers的read操作,返回levels、loggers、groups

loggerLevels

代码语言:javascript复制
	@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;
	}

LoggersEndpoint定义了loggerLevels的read操作,它接受name,返回对应的GroupLoggerLevels或者SingleLoggerLevels

configureLogLevel

代码语言:javascript复制
	@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定义了configureLogLevel这个write操作,它可用于变更logger的级别

小结

springboot的actuator定义了LoggersEndpoint,它定义了loggers的read操作,返回levels、loggers、groups;定义了loggerLevels的read操作,它接受name,返回对应的GroupLoggerLevels或者SingleLoggerLevels;定义了configureLogLevel这个write操作,可用于变更logger的级别。

0 人点赞