3

I am using hibernate as ORM tool. It's working properly, but the problem is that, I am getting the hibernate logs on the console instead I want then in a log file. I tried with the following code.

LogManager.resetConfiguration();
PropertyConfigurator.configureAndWatch("D:\\Workspace\\Project\\log4j.properties");

and the property file looks like :

log4j.rootLogger=FATAL, HIBERNATEFILE

log4j.appender.HIBERNATEFILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HIBERNATEFILE.file=H:/Project/Hibernate.log
log4j.appender.HIBERNATEFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.HIBERNATEFILE.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} %c | %m%n

log4j.category.org.hibernate=DEBUG, HIBERNATEFILE
log4j.additivity.org.hibernate=false

The Java code, I have written before I call hibernate to create configuration. I get all the statements, which I have logged in my project properly, but the hibernate logs, I need on a log file.

Am I missing something?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
M.J.
  • 16,266
  • 28
  • 75
  • 97

2 Answers2

3

Take a look here or at the documentation, you need to tell log4j what package/category to look at.

example...

log4j.logger.org.hibernate=HIBERNATEFILE
log4j.additivity.org.hibernate.SQL=false
log4j.logger.org.hibernate.type=TRACE,HIBERNATEFILE
Community
  • 1
  • 1
Michael J. Lee
  • 12,278
  • 3
  • 23
  • 39
  • i I tried with what u have suggested. but it didn't worked. still getting the logs on console. – M.J. Jul 14 '11 at 10:53
  • Could you update your answer to include both the logging category AND appender for that category? – Michael J. Lee Jul 14 '11 at 11:08
  • I'm updated my example to be a little more direct. Are you getting any errors when you start your application like... please init. log4j correctly? Also how is this running - is it a web-app, swing-app, etc, some application servers can be tricky to get working. – Michael J. Lee Jul 14 '11 at 11:38
1

I believe the Hibernate output to console is caused by you setting hibernate.show_sql to true. You should turn it off and configure Hibernate to log SQL to the log4j/sl4j log.

Olaf
  • 6,249
  • 1
  • 19
  • 37