使用Spring Boot的LoggersEndpoint管理日志级别
作者:codecraft
这篇文章主要为大家介绍了使用Spring Boot的LoggersEndpoint管理日志级别,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
序
本文主要研究一下springboot的LoggersEndpoint
LoggersEndpoint
/** * {@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
@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
@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
@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的级别。
以上就是使用Spring Boot的LoggersEndpoint管理日志级别的详细内容,更多关于springboot LoggersEndpoint的资料请关注脚本之家其它相关文章!