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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答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
回答2:
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.
回答3:
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>