spring-boot 利用 actuator 动态设置 logging 的日志级别

引子

已经上线的服务通常情况下都会关闭日志打印功能。但是一但进行排错的时候,又必须开启日志输出,而修改日志级别的方式有多种。这里只说明个人认为最优的方式

依赖

spring-boot、spring-boot-actuator

方式

定义一个根 endpoint

import java.util.concurrent.atomic.AtomicBoolean;

import org.springframework.boot.actuate.endpoint.AbstractEndpoint;

public class MyLogEndpoint extends AbstractEndpoint<Boolean> {

    AtomicBoolean atomicBoolean = new AtomicBoolean();

    // mgrLogging 为该 endpoint 的请求子路径:  http://localhost:8080/mgrLogging
    public MyLogEndpoint() {
        super("mgrLogging", true, true);
    }

    @Override
    public Boolean invoke() {
        // 这里我的本意是 log 开关,一个布尔值。
        // 每次请求 http://localhost:8080/mgrLogging 都会调用该 invoke 方法
        // 通常情况下返回该 endpoint 的监控信息
        return atomicBoolean.getAndSet(!atomicBoolean.get());
    }

}

定义一个挂靠在根 endpoint 的子节点

import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.logback.LogbackLoggingSystem;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

public class MyLogMvcEndpoint extends EndpointMvcAdapter {

    String format = "change package name [%s] logging level to [%s]";

    public MyLogMvcEndpoint(MyLogEndpoint delegate) {
        super(delegate);
    }

    // 注意该 path 不是 http://localhost:8080/{level}/{pkn}
    // 而是构造方法中 MyLogEndpoint 的 id 对应 path 下的请求节点。
    // http://localhost:8080/mgrLogging/{level}/{pkn}

    @RequestMapping(value = "/{level}/{pkn}")
    @ResponseBody
    public Object changeLog(@PathVariable LogLevel level, @PathVariable("pkn") String packageName) {
        System.err.println(String.format(format, packageName, level));

        try {
            // 处理日志 level 改变逻辑,根据个人需求改变
            LogbackLoggingSystem logbackLoggingSystem = new LogbackLoggingSystem(this.getClass().getClassLoader());
            logbackLoggingSystem.setLogLevel(packageName, level);

            // 处理成功(未抛出异常)返回 success
            return "success";
        } catch (Exception e) {
            // 处理失败返回异常信息
            return e.getMessage();
        }
    }

}

endpoint 注册

@Configuration
@ConditionalOnWebApplication
public class MyLogConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public MyLogEndpoint changeLogEndpoint() {
        return new MyLogEndpoint();
    }

    @Bean
    @ConditionalOnBean(MyLogEndpoint.class)
    public MyLogMvcEndpoint changeLogMvcEndpoint(MyLogEndpoint delegate) {
        return new MyLogMvcEndpoint(delegate);
    }

}

最后

org.springframework.boot.actuate.endpoint.Endpoint
该 url 不能接受请求参数,一般是返回该 endpoint 监控的信息

org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint
该 url 可接受参数,一般情况下是用来修改或查询该 endpoint 的配置。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值