views:

467

answers:

1

I have been using Hibernate very successfully, but today I noticed a bizarre phenomenon to do with logging. If I turn on logging (at a "debug" level) for the classes underneath org.hibernate (for instance org.hibernate.SQL or org.hibernate.impl.SessionImpl), I get twice each log line corresponding to those classes. I never see that with logging any other package.

Here's an excerpt from a log output. The first and fourth lines are mine, the others are Hibernate's:

23:54:45,472 TRACE UserDaoImpl:37 - Loading User by id: 2
23:54:45,473 DEBUG SQL:401 - select user0_.id as id22_2_, user0_.avatar as avatar22_2_, ...
23:54:45,473 DEBUG SQL:401 - select user0_.id as id22_2_, user0_.avatar as avatar22_2_, ...
23:54:45,499 TRACE PropertyDaoImpl:40 - Loading class com.myapp.locale.Region with ID 21
23:54:45,503 DEBUG SQL:401 - select region0_.id as id19_1_, region0_.code as code19_1_, ...
23:54:45,503 DEBUG SQL:401 - select region0_.id as id19_1_, region0_.code as code19_1_, ...

Evidence from the side of the database shows that the commands are sent only once each (fortunately!).

I use Hibernate-Annotations 3.3.1.GA
with the PostgreSQL JDBC connector 5.0.7
and log4j 1.2.14

+3  A: 

All I can think of:

  1. Check that your log4j config doesn't have multiple appenders defined at different levels? If you have your appender attached to the org.hibernate logger, and attached again to the org.hibernate.SQL logger, then you'll see this twice (unless you turn off additivity for the lower-level logger).
  2. You don't have the hibernate.show_sql property set on your SessionFactory? That doesn't enable logging to log4j (you do that through log4j config), it just dumps to the console. If your log4j is dumping to the console too you'll see it twice. However, that looks like a log4j format log line, not the console dumps done by hibernate, so that seems unlikely. Worth mentioning though.
Cowan
My log4j.properties contained 2 lines concerning Hibernate: one setting org.hibernate to "warn" and the other org.hibernate.SQL to "debug". Commenting out the first of these solved the issue. Thanks, Cowan! (I'll now read about "additivity"...)
pierdeux