File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Open Source Projects and the fly likes log4j not working with JUnit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "log4j not working with JUnit" Watch "log4j not working with JUnit" New topic

log4j not working with JUnit

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Hi All,
I have a very strange problem. I hope someone else has seen this before. I have a class that uses log4j, and it works fine if I run the class by itself. I use PropertyConfigurator.configure("MyFile.ini"); However, if I run JUnit on this class, I get tons of errors like this:

If I use BasicConfigurator.confiure() in the tested class, then JUnit will run it just fine.
Very strange! Has anyone seen this problem before?
Many thanks to you,
Brian Buckley

Joined: Jan 12, 2003
Posts: 7
I am seeing the identical problem. If anyone knows areason, a solution or a workaround, please post.
Calvin Robol

Joined: Feb 01, 2002
Posts: 3
There is a workaround that will work with log4j, however it requires you to rebuild/re-jar JUnit.
In the junit.jar, there is a file called This lists all the packages you want to exclude from the Junit custom class loader, which I believe is causing the problem with log4j. Just add excluded.9=org.apache.log4j.*
to the list, rebuild junit, and log4j should work fine within your junit tests.
Olaf Kittelmann

Joined: Jan 23, 2003
Posts: 1
I just used the workaround and now the
error with the double class loader is gone.
however, my log4j output is also gone.
I stepped through the log4j code and found that it tries to apply INFO level for my class although the rootlogger and the class explicitly are set to DEBUG level in the logger properties.
I helped myself with system.out statements, but
I am not really happy about it.
[ January 23, 2003: Message edited by: Olaf Kittelmann ]
Henrik Bladh

Joined: Mar 06, 2003
Posts: 1
I've had the same problem and I solved it by passing "-Dlog4j.ignoreTCL=true" to the jvm.
I agree. Here's the link:
subject: log4j not working with JUnit
It's not a secret anymore!