Enable logging for Spring own components under Jetty

I just wanted to share how to enable logging for Spring own components when running a Web application under Jetty (using jetty-maven-plugin). I spent a lot of time trying to figure this out so if you are lost like I was, keep reading.

By default, Spring uses Jakarta Commons Logging (JCL) framework, whereas Jetty uses SLF4J. The trick is to configure Spring to use JCL-to-SLF4 bridge which will delegate all JCL logging to SLF4J. This can be accomplished by declaring the following dependencies in the POM:

<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>jcl-over-slf4j</artifactId>
	<version>${slf4j.version}</version>
	<scope>runtime</scope>		
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-log4j12</artifactId>
	<version>${slf4j.version}</version>
	<scope>runtime</scope>		
</dependency>

where ${slf4j.version} is a property that that defines the SLF4J version, i.e:

<properties>
	<slf4j.version>1.7.5</slf4j.version>
</properties>

It is very important that these SLF4J dependencies are not declared as Jetty plugin dependencies. They must be declared for the entire POM, otherwise this is not going to work.

Next, here is my Jetty plugin declaration:

<plugin>
	<groupId>org.eclipse.jetty</groupId>
	<artifactId>jetty-maven-plugin</artifactId>
	<version>9.0.4.v20130625</version>
	<configuration>
		<webApp>				
			<contextPath>/atgalaxy</contextPath>
		</webApp>				
		<connectors>
			<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
				<port>8080</port>
			</connector>
		</connectors>
	</configuration>
</plugin>

The above setup was tested with Spring 3.2.3.RELEASE, SLF4J 1.7.5 and jetty-maven-plugin 9.0.4.v20130625.

This entry was posted in Spring. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


− 4 = four

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>