I have a Spring Application which is using JPA/Hibernate. I want to track every insert and update statement in the DB. Is it possible to log the data update to a file and output a new file everyday. Basically if something changes in the database I want to track it in a log file. And there needs to be one log file everyday. I do have log4.xml in my application. Besides this I dont know where to begin. Any suggestions. tips solutions and pointers to good references are greatly appreciated.
Asked
Active
Viewed 2,263 times
3 Answers
1
It is possible to log every sql fired by hibernate. you can have a rolling file configuration to get a new file every day. refer: http://logging.apache.org/log4j/1.2/manual.html I hope this helps.

Prakash
- 461
- 1
- 5
- 21
1
In the hibernate configuration, set show sql property to true and then redirect hibernate logs to a file using some logging API
<property name="show_sql">true</property>
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
References : Hibernate show real SQL
-
where do i add this properties to the appender above. I have set the Show_sql to true. – user3078701 Sep 18 '14 at 01:39
-
ok. I can get the SQL to log but in the insert statement I dont see the actual insert data. What I see is this ibernate: /* insert
*/ insert into case_versions ( – user3078701 Sep 18 '14 at 06:53) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) Hibernate: Its a bunch of Question marks and not the actual data. -
also the SQL is logging into console as well. Which is what I dont want to happen. – user3078701 Sep 18 '14 at 06:56
-
Please see the reference link, it explains things well. – ares Sep 18 '14 at 10:15
0
it is very easy, add a new appender to your log4j config file.
Example:
<appender name="RollingAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="app.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d %c %M - %m%n"/>
</layout>
</appender>

Jaiwo99
- 9,687
- 3
- 36
- 53