[英]Get the logging level in spring boot application
我創建了一個Benchmark
注釋,它給出了使用Stopwatch
的方法調用的執行時間。 Benchmark
注解有一個isEnabled
屬性,默認為true
。 我想要的是,即使某些方法的值設置為false
,如果日志記錄級別是DEBUG
,也可以完成基准測試。 spring 啟動中有沒有辦法獲取應用程序的當前日志記錄級別。 我知道我們可以為不同的包啟用多個日志記錄級別。 如果對於當前的 package 或模塊,日志級別設置為 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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.