Search code examples
spring-bootloggingspring-boot-starter

Spring Boot 2: set loglevel in custom starter?


Is it possible to set the loglevel as part of the configuration of a custom starter?

Lets say that I've, for example, created a custom starter that extends the default spring-boot-starter-data-jpa. Is it possible to set the loglevel for org.hibernate to WARN as part of the starter so that every application that uses my custom starter automatically inherits this without having to switch from INFO to WARN in their own properties file?

Thanks alot!


Solution

  • I'd recommend doing this by adding a property source to the environment that contains the logging.level.org.hibernate property set to WARN.

    To do so, you can use META-INF/spring.factories to register an implementation of EnvironmentPostProcessor. The spring.factories file is a properties file. The keys are the fully qualified class name of the implemented interface. In this case it's org.springframework.boot.env.EnvironmentPostProcessor and the value should be the fully qualified name of your implementation. The contents would like something like this:

    org.springframework.boot.env.EnvironmentPostProcessor=com.example.HibernateLoggingEnvironmentPostProcessor
    

    In your EnvironmentPostProcessor implementation you should add a PropertySource to the Environment. That property source should contain a logging.level.org.hibernate property with a value of WARN. For example, you could use a MapPropertySource created using Collections.singletonMap.