前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊springboot2的LoggersEndpoint

聊聊springboot2的LoggersEndpoint

作者头像
code4it
发布2018-09-17 16:13:10
8640
发布2018-09-17 16:13:10
举报
文章被收录于专栏:码匠的流水账

本文主要研究下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.example HTTP/1.1 204 Date: 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

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

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

小结

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

doc

  • Spring Boot Reference Guide
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-04-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实例
  • LoggersEndpointAutoConfiguration
  • LoggersEndpoint
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档