How do I set the logging properties in a spring java configuration?

久未见 提交于 2020-01-23 03:23:05

问题


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 

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?


回答1:


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

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

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



来源:https://stackoverflow.com/questions/46636599/how-do-i-set-the-logging-properties-in-a-spring-java-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!