Log file to track JPA/Hibernate updates/Insert eve

2019-06-13 03:49发布

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.

3条回答
冷血范
2楼-- · 2019-06-13 04:16

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

查看更多
【Aperson】
3楼-- · 2019-06-13 04:18

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.

查看更多
干净又极端
4楼-- · 2019-06-13 04:20

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>
查看更多
登录 后发表回答