0
votes

I like to use SLF4J (with the log4j binding) for most of my projects' logging system. I'm rolling up my sleeves with Hibernate for the very first time, and see that one can enable/disable the printing of SQL statements (as executed by Hibernate) by using the following property in your hibernate.cfg.xml file:

<property name="show_sql">true</property>

Is there any way to specify this inside my log4j config XML instead of inside the Hibernate config? I'd like to unify all of my logging settings into one configuration file.

1
Thanks @Reimeus (+1) - can you elaborate a bit more, especially when you say "...destined for 2 different objects."? In my mind, if show_sql is true, then Hibernate logs the SQL statements to STDOUT (I believe - but please correct me if I'm wrong there). So it would be nice to define where STDOUT goes from inside slf4j/log4j, since they are my logging mechanism, and show_sql falls into the domain/realm of logging. Thanks again!IAmYourFaja
Agreed that SLF4J does not (and should not) know anything about the content of what's being logged, but a strongsuit of a centralized logging system (like log4j) is to be able to say "hey, take all STDOUT output and send it to myapplog.txt." So I'm wondering here if the solution is to just keep show_sql always turned on, and then let log4j determine what to do with STDOUT? Is that a viable solution?IAmYourFaja
Thanks again @Reimeus (+1) - last followup: do you know of a way to instruct Hibernate to dynamically turn SQL logging on/off? That way I could expose, say, a JMX Bean and turn SQL logging on if I needed to begin a diagnostic session (of course, at the cost of diminished performance).IAmYourFaja

1 Answers

3
votes

Typically they're done by setting following logger into DEBUG (via log4j.properties file). You don't even need to change hibernate.cfg.xml

log4j.logger.org.hibernate.SQL=DEBUG