Log file to track JPA/Hibernate updates/Insert every day

丶灬走出姿态 提交于 2019-12-10 12:22:35

问题


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>


来源:https://stackoverflow.com/questions/25896998/log-file-to-track-jpa-hibernate-updates-insert-every-day

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!