gotch4
gotch4

Reputation: 13269

How to implement database operations logging in Hibernate + Spring

I'd like to be able to log somewhere (maybe a table) every inserte/update/delete in the db. I'm using spring + hibernate. I'm open to a solution based on either or both.

Any ideas?

Edit

I didn't mean textual logging, but actually to intercept the operations or have them logged in some data structure that can be processed.

Upvotes: 0

Views: 172

Answers (2)

codependent
codependent

Reputation: 24452

If you don't need the actual SQL sentences, you can use an aspect over you DAOs. This way you can intercept every data access operation and insert a register in a DATA_ACCESS_LOG table describing the operation and the parameters passed.

Upvotes: 0

codependent
codependent

Reputation: 24452

If logging to a properties file is OK to you just add in your log4j.xml config:

<category name="org.hibernate.SQL" additivity="false">
    <priority value="trace"/>
    <appender-ref ref='file'/>
</category>

With log4j.properties:

log4j.category.org.hibernate.SQL=TRACE, file

where "file" is an appender defined like this:

log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=SOME_PATH/sql_log.log
log4j.appender.file.Append=true
log4j.appender.file.encoding=UTF-8
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss,SSS}  [%-9t]  %-5p  %c{1}.%m%n

Upvotes: 2

Related Questions