Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: Why my log4j doesn't work in J2EE app?

 
Choon-Chern Lim
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed to get log4j to work with hibernate in java stand-alone app, but when I incorporate it in J2EE app, it doesn't display in nice beautiful formatted hibernate messages anymore in the console.

This is what I have in my log4j.properties:-


This is my folder structure:-



When I run the J2EE app, my log looks something like this:-



Am I missing out something?

Thanks.
 
Paul Clapham
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed exactly the same thing happening in my Websphere installation. Here is what I think is causing it:

Hibernate uses Apache commons-logging to log those messages. And commons-logging will use log4j, if it sees it. But commons-logging is built into the heart of Websphere (if you left the DefaultApplication running, goto http://yourwebsphere/snoop and you can see that). From there it can't see your log4j jar in your web app, because that's in a classloader that's a child of the main Websphere classloader.

You can't fix the problem by putting log4j in websphere/appserver/lib, because then it won't be able to find your log4j.properties file for the same reason. If you put log4j.properties in websphere/appserver/lib then it will be the log4j properties for the entire server and not just your web app.

And I can also tell you that changing your web app's classloader mode to be "parent last" instead of "parent first" doesn't fix the problem either because I tried that. I even have a copy of the commons-logging jar in my web app but that doesn't help.

Possible solutions would include Hibernate not using commons-logging and Websphere not using commons-logging, but I don't think either of those are likely to happen. Has this issue not been discussed in the Hibernate forum?
 
Paul Clapham
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has this issue not been discussed in the Hibernate forum?[/QB]

As it turns out this was a good question. It has. Here's the link:

http://forum.hibernate.org/viewtopic.php?t=944138

The PDF referred to in the answer is about 20 pages long. I am having a look at it myself. Sounds like it should work.
 
Choon-Chern Lim
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Holy macro! I never ever thought the solution would be that complicated!

I was reading through that pdf... and sounds like majority of the options require me to either re-jar commons-logging.jar, or set the class loader to PARENT_LAST. Geeez... I'm not really fond of doing either step just to get the log4j to work with hibernate in WAS 6.

In fact, I already had to endorse antlr.jar in WAS 6 so that my HQL query to work with throwing nasty exceptions.

Anyway, thanks Paul, I appreciate your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic