Java 在日志中显示 Spring 事务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1965454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing a Spring transaction in log
提问by cometta
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.
我为 spring 配置了事务支持。有没有办法记录交易以确保我正确设置了所有内容?在日志中显示是查看正在发生的事情的好方法。
采纳答案by Bozho
in your log4j.properties
(for alternative loggers, or log4j's xml format, check the docs)
在您的log4j.properties
(对于替代记录器或 log4j 的 xml 格式,请查看文档)
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
根据您的事务管理器,您可以设置 spring 框架的日志记录级别,以便它为您提供有关事务的更多信息。例如,在使用的情况下JpaTransactionManager
,您设置
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
如果INFO
不够,请使用DEBUG
回答by Michel Gokan
Because you can access Spring classes at runtime, you can determine transaction status. This article may help you:
因为您可以在运行时访问 Spring 类,所以您可以确定事务状态。这篇文章可以帮到你:
回答by Pascal Thivent
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:
最有趣的日志信息JtaTransactionManager.java
(如果这个问题仍然是关于JtaTransactionManager
)被DEBUG
优先记录。假设你log4j.properties
在类路径上有一个地方,我建议使用:
log4j.logger.org.springframework.transaction=DEBUG
回答by Pep
You could enable JDBC logging as well:
您也可以启用 JDBC 日志记录:
log4j.logger.org.springframework.jdbc=DEBUG
回答by Sander S.
For me, a good logging config to add was:
对我来说,要添加的一个好的日志配置是:
log4j.logger.org.springframework.transaction.interceptor = trace
log4j.logger.org.springframework.transaction.interceptor = 跟踪
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]
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] 的交易
回答by MariuszS
For Spring Boot application with application.properties
对于 Spring Boot 应用程序 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
)
或者如果你更喜欢 Yaml ( application.yaml
)
logging:
level:
org.springframework.orm.jpa: DEBUG
org.springframework.transaction: DEBUG
回答by David Tonhofer
Here is some code I use in my Logback Layout implementation derived from ch.qos.logback.core.LayoutBase.
这是我在从ch.qos.logback.core.LayoutBase派生的 Logback 布局实现中使用的一些代码。
I create a thread-local variable to store the reference to the method org.springframework.transaction.support.TransactionSynchronizationManager.isActualTransactionActive()
. Whenever a new log line is printed out, getSpringTransactionInfo()
is called and it returns a one-character string that will go into the log.
我创建了一个线程局部变量来存储对方法的引用org.springframework.transaction.support.TransactionSynchronizationManager.isActualTransactionActive()
。每当打印出新的日志行时,getSpringTransactionInfo()
都会调用它并返回一个将进入日志的单字符字符串。
References:
参考:
- Tips for Debugging Spring's @Transactional Annotation
- org.springframework.transaction.support.TransactionSynchronizationManager
- java.lang.ThreadLocal
- java.lang.Class.getMethod()
- 调试 Spring 的@Transactional 注解的技巧
- org.springframework.transaction.support.TransactionSynchronizationManager
- java.lang.ThreadLocal
- java.lang.Class.getMethod()
Code:
代码:
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;
}