简体   繁体   English

获取 spring 引导应用程序中的日志记录级别

[英]Get the logging level in spring boot application

I have created a Benchmark annotation that gives the execution time of a method call using Stopwatch .我创建了一个Benchmark注释,它给出了使用Stopwatch的方法调用的执行时间。 The Benchmark annotation has an attribute isEnabled which is true by default. Benchmark注解有一个isEnabled属性,默认为true What I want is that even if the value if set to false for some method, if the logging level is DEBUG , the benchmarking be done.我想要的是,即使某些方法的值设置为false ,如果日志记录级别是DEBUG ,也可以完成基准测试。 Is there a way in spring boot to get the current logging level of the application. spring 启动中有没有办法获取应用程序的当前日志记录级别。 I am aware that we can have multiple logging levels enabled for different packages.我知道我们可以为不同的包启用多个日志记录级别。 If for the current package or module, the logging level is set to DEBUG, the benchmark should be executed.如果对于当前的 package 或模块,日志级别设置为 DEBUG,则应执行基准测试。 This is how my Benchmark annotation looks like:这就是我的Benchmark注释的样子:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Benchmark {

    boolean isEnabled() default true;
}

The aspect that does the benchmarking looks like this:进行基准测试的方面如下所示:

@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();
        }
    }
}

I think you can just do;我认为你可以这样做;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM