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