问题
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