Reputation: 6689
I am developing application that serves REST API for clients, to do this I decided to use JAX-RS
implementation Jersey
and Spring
for some dependency injection.
Today I noticed in my logs, that my application context seems to be loaded twice, for example:
2013-12-21 22:18:55 INFO ContextLoader:272 - Root WebApplicationContext: initialization started
2013-12-21 22:18:55 INFO ContextLoader:272 - Root WebApplicationContext: initialization started
I looked through similiar posts, but seeing my web.xml
file, I can't seem to find the culprit:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>RestPowtorkiServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>org.powtorki.spring.PowtorkiApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestPowtorkiServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!--
Apply Spring Security Filter to all REST Requests
-->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/rest/*</url-pattern>
</filter-mapping>
Also I tried running example app from Jersey
repository (https://github.com/jersey/jersey/tree/2.4.1/examples/helloworld-spring-webapp) and it seems it is suffering from the same issue.
Is it a bug in Spring
integration with Jersey
? If so is there a way to fix it somehow?
EDIT: log4j configuration:
# Root logger option
log4j.rootLogger=DEBUG, stdout
log4j.category.org.springframework.data.document.mongodb=DEBUG,stdout
log4j.logger.org.springframework.web.context.ContextLoader=DEBUG,stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Upvotes: 1
Views: 526
Reputation: 280141
There is a log4j Appender
property known as additivity. It is described as
The output of a log statement of logger C will go to all the appenders in C and its ancestors. This is the meaning of the term "appender additivity".
However, if an ancestor of logger C, say P, has the additivity flag set to false, then C's output will be directed to all the appenders in C and its ancestors upto and including P but not the appenders in any of the ancestors of P.
Therefore your statements logged on org.springframework.web.context.ContextLoader
will also go to rootLogger
which is its parent.
You can set its additivity flag to false
by putting
log4j.additivity.org.springframework.web.context.ContextLoader=false
Upvotes: 3