java - 获取 Spring Boot 应用程序中的日志记录级别
问题描述
我创建了一个Benchmark
注释,它使用Stopwatch
. Benchmark
注释有一个isEnabled
默认true
属性。我想要的是,即使该值设置false
为某种方法,如果日志记录级别为DEBUG
,则进行基准测试。spring boot 中有没有办法获取应用程序的当前日志记录级别。我知道我们可以为不同的包启用多个日志记录级别。如果对于当前包或模块,日志记录级别设置为 DEBUG,则应执行基准测试。这就是我的Benchmark
注释的样子:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Benchmark {
boolean isEnabled() default true;
}
进行基准测试的方面如下所示:
@Aspect
@Component
public class BenchmarkingAspect {
@Around("@annotation(benchmark)")
public Object benchmarkMethodRuntimeAspect(ProceedingJoinPoint proceedingJoinPoint, Benchmark benchmark) throws Throwable {
if (benchmark.isEnabled()) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Object returnedValue = proceedingJoinPoint.proceed();
stopWatch.stop();
log.info("Benchmarked: {} from class {}", proceedingJoinPoint.getSignature().getName(),
proceedingJoinPoint.getTarget().getClass().getCanonicalName());
log.info(stopWatch.prettyPrint());
return returnedValue;
} else {
return proceedingJoinPoint.proceed();
}
}
}
解决方案
我认为你可以这样做;
Logger targetLogger = LoggerFactory.getLogger(proceedingJoinPoint.getTarget().getClass())
if (targetLog.isDebugEnabled()) {
// apply your logic here
}
推荐阅读
- flutter - 需要帮助在颤振代码中复制设计
- java - 如何解决 tanukisoftware 包装器中的“WrapperSimpleApp: Unable to locate the class ...”
- sql - 连接两个表并获取女性计数大于男性计数的部门名称
- python - 使用 python 改进将数据帧加载到 postgress db
- .htaccess - .htaccess 重写规则代码也无法正常工作
- python - 我试图使用 tensorflow 在自定义数据集上实现对象检测
- ios - 如何在导航栏swift ui中设置左右按钮(前导/尾随)?
- reactjs - 提交后表单未设置为空
- python - 如何将robot-ide (RIDE) 扩展为有一个选项卡,以便我可以拖放机器人脚本命令?
- java - Java 11 Cloud SDK 的 appengine:run 等价物是什么?