博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊springboot2的LoggersEndpoint
阅读量:6278 次
发布时间:2019-06-22

本文共 4632 字,大约阅读时间需要 15 分钟。

本文主要研究下springboot2的LoggersEndpoint

实例

  • GET /actuator/loggers
{  "levels": [    "OFF",    "ERROR",    "WARN",    "INFO",    "DEBUG",    "TRACE"  ],  "loggers": {    "ROOT": {      "configuredLevel": "INFO",      "effectiveLevel": "INFO"    },    "com": {      "configuredLevel": null,      "effectiveLevel": "INFO"    },    "com.example": {      "configuredLevel": null,      "effectiveLevel": "INFO"    },    "com.example.config": {      "configuredLevel": null,      "effectiveLevel": "INFO"    }  }}复制代码
  • GET /actuator/loggers/com.example
{  "configuredLevel": null,  "effectiveLevel": "INFO"}复制代码
  • POST
curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel": "ERROR"}' http://localhost:8080/actuator/loggers/com.exampleHTTP/1.1 204Date: Wed, 25 Apr 2018 14:54:41 GMT复制代码

LoggersEndpointAutoConfiguration

spring-boot-actuator-autoconfigure-2.0.1.RELEASE-sources.jar!/org/springframework/boot/actuate/autoconfigure/logging/LoggersEndpointAutoConfiguration.java

@Configurationpublic class LoggersEndpointAutoConfiguration {	@Bean	@ConditionalOnBean(LoggingSystem.class)	@Conditional(OnEnabledLoggingSystemCondition.class)	@ConditionalOnMissingBean	@ConditionalOnEnabledEndpoint	public LoggersEndpoint loggersEndpoint(LoggingSystem loggingSystem) {		return new LoggersEndpoint(loggingSystem);	}	static class OnEnabledLoggingSystemCondition extends SpringBootCondition {		@Override		public ConditionOutcome getMatchOutcome(ConditionContext context,				AnnotatedTypeMetadata metadata) {			ConditionMessage.Builder message = ConditionMessage					.forCondition("Logging System");			String loggingSystem = System.getProperty(LoggingSystem.SYSTEM_PROPERTY);			if (LoggingSystem.NONE.equals(loggingSystem)) {				return ConditionOutcome.noMatch(message.because("system property "						+ LoggingSystem.SYSTEM_PROPERTY + " is set to none"));			}			return ConditionOutcome.match(message.because("enabled"));		}	}}复制代码

这里根据loggingSystem,来创建LoggersEndpoint;另外还使用了OnEnabledLoggingSystemCondition

LoggersEndpoint

spring-boot-actuator-2.0.1.RELEASE-sources.jar!/org/springframework/boot/actuate/logging/LoggersEndpoint.java

@Endpoint(id = "loggers")public class LoggersEndpoint {	private final LoggingSystem loggingSystem;	/**	 * Create a new {@link LoggersEndpoint} instance.	 * @param loggingSystem the logging system to expose	 */	public LoggersEndpoint(LoggingSystem loggingSystem) {		Assert.notNull(loggingSystem, "LoggingSystem must not be null");		this.loggingSystem = loggingSystem;	}	@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)); return result; } @ReadOperation public LoggerLevels loggerLevels(@Selector String name) { Assert.notNull(name, "Name must not be null"); LoggerConfiguration configuration = this.loggingSystem .getLoggerConfiguration(name); return (configuration == null ? null : new LoggerLevels(configuration)); } @WriteOperation public void configureLogLevel(@Selector String name, @Nullable LogLevel configuredLevel) { Assert.notNull(name, "Name must not be empty"); this.loggingSystem.setLogLevel(name, configuredLevel); } private NavigableSet
getLevels() { Set
levels = this.loggingSystem.getSupportedLogLevels(); return new TreeSet<>(levels).descendingSet(); } private Map
getLoggers( Collection
configurations) { Map
loggers = new LinkedHashMap<>(configurations.size()); for (LoggerConfiguration configuration : configurations) { loggers.put(configuration.getName(), new LoggerLevels(configuration)); } return loggers; } /** * Levels configured for a given logger exposed in a JSON friendly way. */ public static class LoggerLevels { private String configuredLevel; private String effectiveLevel; public LoggerLevels(LoggerConfiguration configuration) { this.configuredLevel = getName(configuration.getConfiguredLevel()); this.effectiveLevel = getName(configuration.getEffectiveLevel()); } private String getName(LogLevel level) { return (level == null ? null : level.name()); } public String getConfiguredLevel() { return this.configuredLevel; } public String getEffectiveLevel() { return this.effectiveLevel; } }}复制代码

通过loggingSystem.getLoggerConfigurations()获取Collection,然后getLoggers方法将configurations转换为Map<String, LoggerLevels>

小结

LoggersEndpoint提供两个readOperation和一个writeOperation,分别用来读取和更改logger的level,非常实用。

doc

转载地址:http://ocfva.baihongyu.com/

你可能感兴趣的文章
css面试题
查看>>
Vue组建通信
查看>>
用CSS画一个带阴影的三角形
查看>>
前端Vue:函数式组件
查看>>
程鑫峰:1.26特朗.普力挺美元力挽狂澜,伦敦金行情分析
查看>>
safari下video标签无法播放视频的问题
查看>>
01 iOS中UISearchBar 如何更改背景颜色,如何去掉两条黑线
查看>>
对象的继承及对象相关内容探究
查看>>
Spring: IOC容器的实现
查看>>
Serverless五大优势,成本和规模不是最重要的,这点才是
查看>>
Nginx 极简入门教程!
查看>>
iOS BLE 开发小记[4] 如何实现 CoreBluetooth 后台运行模式
查看>>
Item 23 不要在代码中使用新的原生态类型(raw type)
查看>>
为网页添加留言功能
查看>>
JavaScript—数组(17)
查看>>
Android 密钥保护和 C/S 网络传输安全理论指南
查看>>
以太坊ERC20代币合约优化版
查看>>
Why I Began
查看>>
同一台电脑上Windows 7和Ubuntu 14.04的CPU温度和GPU温度对比
查看>>
js数组的操作
查看>>