120

我为 spring 配置了事务支持。有什么方法可以记录事务以确保我正确设置了所有内容?在日志中显示是查看正在发生的事情的好方法。

4

7 回答 7

109

在您的log4j.properties(对于替代记录器或 log4j 的 xml 格式,请查看文档)

根据您的事务管理器,您可以设置 spring 框架的日志记录级别,以便它为您提供有关事务的更多信息。例如,在使用的情况下JpaTransactionManager,您设置

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

(这是您的事务管理器的包),还有

log4j.logger.org.springframework.transaction=INFO

如果INFO还不够,请使用DEBUG

于 2009-12-27T09:23:15.983 回答
76

对我来说,要添加的一个好的日志配置是:

log4j.logger.org.springframework.transaction.interceptor = 跟踪

它会向我显示这样的日志:

2012-08-22 18:50:00,031 TRACE - 获取 [com.MyClass.myMethod] 的交易

[我自己的来自方法 com.MyClass.myMethod 的日志语句]

2012-08-22 18:50:00,142 TRACE - 完成 [com.MyClass.myMethod] 的交易

于 2012-08-22T15:58:37.803 回答
56

对于 Spring Boot 应用程序application.properties

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

或者如果您更喜欢 Yaml ( application.yaml)

logging:
   level:
      org.springframework.orm.jpa: DEBUG
      org.springframework.transaction: DEBUG
于 2017-07-20T20:09:48.023 回答
9

最有趣的日志信息JtaTransactionManager.java(如果这个问题仍然是关于JtaTransactionManager)被DEBUG优先记录。假设你log4j.properties在类路径上有一个地方,我建议使用:

log4j.logger.org.springframework.transaction=DEBUG
于 2009-12-27T11:26:51.987 回答
9

您也可以启用 JDBC 日志记录:

log4j.logger.org.springframework.jdbc=DEBUG
于 2012-03-11T23:24:39.040 回答
6

因为可以在运行时访问 Spring 类,所以可以确定事务状态。这篇文章可以帮助你:

https://dzone.com/articles/monitoring-declarative-transac

于 2009-12-27T08:21:00.663 回答
3

这是我在从ch.qos.logback.core.LayoutBase派生的 Logback Layout 实现中使用的一些代码。

我创建了一个线程局部变量来存储对方法的引用org.springframework.transaction.support.TransactionSynchronizationManager.isActualTransactionActive()。每当打印出新的日志行时,getSpringTransactionInfo()都会调用它并返回一个将进入日志的单字符字符串。

参考:

代码:

private static ThreadLocal<Method> txCheckMethod;

private static String getSpringTransactionInfo() {
    if (txCheckMethod == null) {
        txCheckMethod = new ThreadLocal<Method>() {
            @Override public Method initialValue() {           
                try {
                    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
                    Class<?> tsmClass = contextClassLoader.loadClass("org.springframework.transaction.support.TransactionSynchronizationManager");
                    return tsmClass.getMethod("isActualTransactionActive", (Class<?>[])null);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }                      
            }
         };    
    }
    assert txCheckMethod != null;
    Method m = txCheckMethod.get();
    String res;
    if (m == null) {
        res = " "; // there is no Spring here
    }
    else {
        Boolean isActive = null;
        try {
            isActive = (Boolean) m.invoke((Object)null);
            if (isActive) {
                res = "T"; // transaction active                    
            }
            else {
                res = "~"; // transaction inactive
            }
        }
        catch (Exception exe) {
            // suppress 
            res = "?";
        }
    }
    return res;
}
于 2017-10-10T08:53:09.867 回答