使用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,它構(gòu)造器依賴loggingSystem及l(fā)oggerGroups
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,返回對應(yīng)的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的級別
小結(jié)
springboot的actuator定義了LoggersEndpoint,它定義了loggers的read操作,返回levels、loggers、groups;定義了loggerLevels的read操作,它接受name,返回對應(yīng)的GroupLoggerLevels或者SingleLoggerLevels;定義了configureLogLevel這個write操作,可用于變更logger的級別。
以上就是使用Spring Boot的LoggersEndpoint管理日志級別的詳細(xì)內(nèi)容,更多關(guān)于springboot LoggersEndpoint的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解在Spring Boot框架下使用WebSocket實(shí)現(xiàn)消息推送
這篇文章主要介紹了詳解在Spring Boot框架下使用WebSocket實(shí)現(xiàn)消息推送,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-12-12
SpringBoot集成WebSocket實(shí)現(xiàn)雙屏實(shí)時消息互推功能
在項(xiàng)目開發(fā)中,實(shí)時消息推送是高頻需求,比如雙屏聯(lián)動、大屏監(jiān)控、在線聊天、訂單狀態(tài)推送等場景,本文以SpringBoot 2.7.x(最穩(wěn)定版本,零基礎(chǔ)友好)為基礎(chǔ),手把手教大家從 0 到 1 集成 WebSocket,實(shí)現(xiàn)左屏 / 右屏雙端實(shí)時消息互推功能,需要的朋友可以參考下2026-02-02
Kafka單節(jié)點(diǎn)偽分布式集群搭建實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Kafka單節(jié)點(diǎn)偽分布式集群搭建實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
通過實(shí)例了解Java jdk和jre的區(qū)別
這篇文章主要介紹了通過實(shí)例了解Java jdk和jre的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
java核心編程之文件過濾類FileFilter和FilenameFilter
這篇文章主要為大家詳細(xì)介紹了java文件過濾類FileFilter和FilenameFilter,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
postman測試post請求參數(shù)為json類型的實(shí)例講解
下面小編就為大家分享一篇postman測試post請求參數(shù)為json類型的實(shí)例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03

