1

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?

mmaceachran
  • 3,178
  • 7
  • 53
  • 102
  • Store it in the properties object and load the object on startup. Similar to the old fashion approach ? – user641887 Oct 08 '17 at 22:30
  • What properties object? I can certainly make one. How do I annotate/load it? – mmaceachran Oct 08 '17 at 22:56
  • Do you actually want to get a pure java configuration? or you just cant make `application.properties` work? i mean `application.properties` is such a clean and straightforward way for lots of setup – varren Oct 09 '17 at 17:19

1 Answers1

7

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

varren
  • 14,551
  • 2
  • 41
  • 72
  • This answer is Spring Boot-oriented, as well as the answer you provided a link for, which the original question wasn't (only mentioned Spring 5 / Spring MVC, with relevant tags); without Boot, you can't do `new SpringApplicationBuilder()` – maxxyme Jan 19 '22 at 17:43