使用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的资料请关注脚本之家其它相关文章!
相关文章
springboot整合H2内存数据库实现单元测试与数据库无关性
本篇文章主要介绍了springboot整合H2内存数据库实现单元测试与数据库无关性,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2018-01-01Java实现AES/CBC/PKCS7Padding加解密的方法
这篇文章主要介绍了Java实现AES/CBC/PKCS7Padding加解密的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-08-08java 后台开发中model与entity(实体类)的区别说明
这篇文章主要介绍了java 后台开发中model与entity(实体类)的区别说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-08-08java 运行报错has been compiled by a more recent version of the J
java 运行报错has been compiled by a more recent version of the Java Runtime (class file version 54.0)2021-04-04
最新评论