[slf4j-user] Got a spring error when delegate log4j&Jul to slf4j then to log4j.

Jason Wang jason.wang at bulletin.net
Mon Jun 29 03:37:02 CEST 2009


Hi all,

I am brand new to this wonderful slf4j tool(started to read about it 2 
hours ago) and from my understanding switching to it should be a pain 
free process.  However I ran into some problems.

My project mainly uses log4j directly and it now depends on a 3rd party 
library which uses JUL as  its logging framework. I would like to make 
sure all the logs the 3rd party lib produces under the control of my 
log4j settings. After a quick search on Google, the best approach seems 
to be use the slf4j lib to bridge JUL to it and then tell slf4j to use 
log4j as its backing implementation.

So my solution idea is:
1.Delegating all of my log4j logging request to slf4j (by using log4j 
over slf4j and removing the log4j jar)
2. Make sure  JUL log requests go to slf4j by using jul-to-slf4j.

3. Make sure slf4j uses log4j's implementation by using lf4j-log4j12. So 
hopefully I get:
   A: my log4j.xml settings still work.
   B: the 3rd party lib's JUL logging requests end up going to log4j in 
the end.

My steps are:

1. comment out the dependency on log4j in my pom.xml, so the log4j.jar 
will no longer be in my webapp/lib

2. introduce the following 3 dependencies.
<dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>log4j-over-slf4j</artifactId>
         <version>1.5.8</version>
       </dependency>
           <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-log4j12</artifactId>
           <version>1.5.8</version>
           <scope>test</scope>
       </dependency>
         <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>jul-to-slf4j</artifactId>
           <version>1.5.8</version>
       </dependency>

After applying the changes, the project still builds, great. But it 
fails immediately on runtime:

Spring complains that it could not initialize org.apache.log4j.logger. 
Detailed message:

 Nested in org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'paypalRestService' defined in file 
[/home/jasonw/dev/bulletin-paymentengine/target/classes/com/bulletin/paymentengine/api/paypal/rest/resource/PaypalRestService.class]: 
Instantiation of bean failed; nested exception is 
java.lang.NoClassDefFoundError: Could not initialize class 
org.apache.log4j.Logger:
java.lang.NoClassDefFoundError: Could not initialize class 
org.apache.log4j.Logger
   at 
com.bulletin.paymentengine.api.paypal.rest.resource.PaypalRestService.<clinit>(PaypalRestService.java:45) 



So, my questions are: will my solution work at all in theory? Is there 
anything wrong in my solution?
How to fix this spring problem?


Thanks alot.

Jas





More information about the slf4j-user mailing list