Reputation: 1896
I have a project where I need the logging mechanism before I start the SpringApplication. How can I achieve that?
I tried to setup my own logging mechanism (LogManager.getLogManager().readConfiguration()), but it is overridden when the spring application starts.
Basically I want to use the same logging mechanism everywhere.
Upvotes: 3
Views: 3114
Reputation: 1896
I manage to solve my issue by removing "spring-boot-starter-logging" dependency from my project and adding 'org.slf4j:slf4j-jdk14:1.7.5' and 'commons-logging:commons-logging:1.1.1'.
I use gradle so in my case I achieve that by:
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-logging"
}
compile("org.springframework.boot:spring-boot-starter-actuator") {
exclude module: "spring-boot-starter-logging"
}
compile('org.slf4j:slf4j-jdk14:1.7.5')
compile('commons-logging:commons-logging:1.1.1')
Removing LoggingApplicationListener and logback.xml didn't worked.
Upvotes: 1
Reputation: 116051
Spring Boot uses LoggingApplicationListener
to configure logging for your application. This listener is one of SpringApplication
's default listeners. To use your own already-configure logging system, you need to configure your SpringApplication
so that it doesn't have this listener. For example, to remove the unwanted listener, while retaining all of the other default listeners:
@SpringBootApplication
public class CustomLoggingApplication {
public static void main(String[] args) {
SpringApplication application =
new SpringApplication(CustomLoggingApplication.class);
Collection<ApplicationListener<?>> listeners =
new ArrayList<ApplicationListener<?>>();
for (ApplicationListener<?> listener: application.getListeners()) {
if (!(listener instanceof LoggingApplicationListener)) {
listeners.add(listener);
}
}
application.setListeners(listeners);
application.run(args);
}
}
Upvotes: 4
Reputation: 2817
You can configure Log4j listener in the web.xml instead of the spring-context.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
So it is up before Spring starts.
Upvotes: 0