【问题标题】:Get the logging level in spring boot application获取 Spring Boot 应用程序中的日志记录级别
【发布时间】:2019-10-18 04:03:30
【问题描述】:

我创建了一个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();
        }
    }
}

【问题讨论】:

    标签: java spring-boot logging


    【解决方案1】:

    我认为你可以做到;

    Logger targetLogger = LoggerFactory.getLogger(proceedingJoinPoint.getTarget().getClass())
    if (targetLog.isDebugEnabled()) {
        // apply your logic here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      • 2020-12-01
      相关资源
      最近更新 更多