如何在spring java配置中设置日志记录属性? [英] How do I set the logging properties in a spring java configuration?

查看:222
本文介绍了如何在spring java配置中设置日志记录属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring 5 MVC应用程序。我试图获得纯Java配置。我注意到我的日志记录无效。我在我的application.properties中有这个:

I am using a Spring 5 MVC application. I am trying to get a pure java configuration going. I notice that my logging is not working. I have this in my application.properties:

logging.level.org.springframework.web=ERROR
logging.level.org.springframework.security=ERROR
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE 

现在我当然没有使用application.properties,所以如何在我的一个中以纯Java方式设置它@Configuration类?

Now I am not using application.properties of course, so how do I set this in a pure Java way in one of my @Configuration classes?

推荐答案

如果您真的想要获得纯Java记录器配置,可以这样设置:

If you actually want to get a pure java logger configuration, you can setup it like this:

public class LoggingInitializer implements ApplicationContextInitializer {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        //suppose you use default logback (ch.qos.logback.classic.LoggerContext)
        LoggerContext c = (LoggerContext) LoggerFactory.getILoggerFactory();
        c.getLogger("ru.varren").setLevel(Level.DEBUG);
        c.getLogger("org.springframework.web").setLevel(Level.ERROR);
        c.getLogger("org.springframework.security").setLevel(Level.ERROR);
        c.getLogger("org.hibernate.SQL").setLevel(Level.DEBUG); 
    }
}

然后在应用程序启动前在main中初始化它。

And then init it in main before the app starts.

public class Main {

    public static void main(String[] args){
        new SpringApplicationBuilder(Main.class)
                .initializers(new LoggingInitializer())
                .run(args);
    }
}

另请看一下这个答案: https://stackoverflow.com/a/20521500/1032167

Also take a look at this answer: https://stackoverflow.com/a/20521500/1032167

这篇关于如何在spring java配置中设置日志记录属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆