Showing a Spring transaction in log

116,272

Solution 1

in your log4j.properties (for alternative loggers, or log4j's xml format, check the docs)

Depending on your transaction manager, you can set the logging level of the spring framework so that it gives you more info about transactions. For example, in case of using JpaTransactionManager, you set

log4j.logger.org.springframework.orm.jpa=INFO

(this is the package of the your transaction manager), and also

log4j.logger.org.springframework.transaction=INFO

If INFO isn't enough, use DEBUG

Solution 2

For me, a good logging config to add was:

log4j.logger.org.springframework.transaction.interceptor = trace

It will show me log like that:

2012-08-22 18:50:00,031 TRACE - Getting transaction for [com.MyClass.myMethod]

[my own log statements from method com.MyClass.myMethod]

2012-08-22 18:50:00,142 TRACE - Completing transaction for [com.MyClass.myMethod]

Solution 3

For Spring Boot application with application.properties

logging.level.ROOT=INFO
logging.level.org.springframework.orm.jpa=DEBUG
logging.level.org.springframework.transaction=DEBUG

or if you prefer Yaml (application.yaml)

logging:
   level:
      org.springframework.orm.jpa: DEBUG
      org.springframework.transaction: DEBUG

Solution 4

You could enable JDBC logging as well:

log4j.logger.org.springframework.jdbc=DEBUG

Solution 5

Most interesting log informations of JtaTransactionManager.java (if this question is still about the JtaTransactionManager) are logged at DEBUG priority. Assuming you have a log4j.properties somewhere on the classpath, I'd thus suggest to use:

log4j.logger.org.springframework.transaction=DEBUG
Share:
116,272
cometta
Author by

cometta

Updated on April 18, 2020

Comments

  • cometta
    cometta about 4 years

    I configured spring with transactional support. Is there any way to log transactions just to ensure I set up everything correctly? Showing in the log is a good way to see what is happening.

  • skaffman
    skaffman over 14 years
    INFO level won't show any tx activity at all, it would be too verbose. DEBUG will be necessary there.
  • Ali
    Ali almost 11 years
    @Bozho I've JpaTransactionManager and I wanna monitor when a connection is borrowed from pool and when it was release for a specific transaction.
  • Bozho
    Bozho almost 11 years
    then you'd need to change the logging configuration for your connection pool
  • Johanneke
    Johanneke about 9 years
    Great! No need to have all the info/debug/trace logging of other packages, when this is what you're looking for :D
  • David Tonhofer
    David Tonhofer over 6 years
    Very broken, but try: Tips for Debugging Spring's @Transactional Annotation (haven't tried it myself yet). It uses TransactionSynchronizationManager to get transaction status. The code should probably use a thread-local variable to cache the reference to the isActualTransactionActive() instead of retrieving it on each logging call.
  • lily
    lily over 4 years
    what if we use mybatis+slf4j+logback+springboot?